BASH – Tips and tricks

Published on Author gryzliLeave a comment

Bash Variable String Manipulation

 

Front Match

These are the examples of a front matching

Front is meant to be “left to right” matching

 

Rear Match

These are the examples of a rear matching

Front is meant to be “right to left” matching

a = "blog.gryzli.info"

echo ${a%.*}
# prints "blog.gryzli"

echo ${a%%.*}
# prints "blog"

 

String substitution

 

stringZ=abcABC123ABCabc

echo ${stringZ/abc/xyz} # xyzABC123ABCabc
# Replaces first match of 'abc' with 'xyz'.

echo ${stringZ//abc/xyz} # xyzABC123ABCxyz
# Replaces all matches of 'abc' with # 'xyz'.

 

Remove Suffix From String

string="domain.com"

# Prints "domain" 
echo ${string%.com}

BASH Arrays

 

One-dimensional arrays

  • Define array:

 

  • Print element from array:

 

 

  • Print the number of elements in array:

 

 

 

  • Check if array value exists

     

 

Adding value to array without specifying index (array push/unshift)

This could be done by the following syntax:

GRYZ_ARR=()

GRYZ_ARR+=('value1')

GRYZ_ARR+=('value2')

This will add values “value1” and “value2” to the array: GRYZ_ARR

 

Working with associative arrays in BASH

Declare associative array

Here it’s very important to use the “declare -A array_name” notation in order to define working associative array. After the array is defined you can use different methods to fulfill it with elements.

 

Iterate through associative array

Now that we have our array defined and fulfilled, we can try to iterate through it’s keys and print it’s values.

 

 

Bash Arithmetic

If you need to make some arithmetics, here are some tools you could use:

 

Using bc

Basic Calculator (bc) is a mathematical scripting language with a syntax  similiar to C.

If you want to sum floating numbers:

 

Using expr

expr is another basic command which could be used to evaluate basic expressions.

Here are some examples

 

Using let

 

 

Unsorted tips and tricks

 

find on same filesystem

Find files and directories, without descending from the finding filesystem.

 

Check if variable is empty

 

Get Symlink destination path

If you want to check the destination of a given symlink, this could be done, by using the command: readlink . The command is part of  the “coreutils” packages, so there is a very big chance, to have it already installed.

 

Random Sleep

Implementing random sleep from 0 to XXXX seconds

 

 

Code blocks

 

Brace expansion

 Integer expansion

 

Print formated columns output

This will issue printf for every line it gets from the $() command. This way it will print well bounded columns.

! Do not use this for very big files, cause the the “argument” will overflow !

 

BASH Modifying Bash Script During Execution

Recently I found some weird behavior of BASH, which will let you append code to bash script, during it’s execution, and the code will get executed as well.

In order to materialize what I mean, here is the nasty example:

 

BASH Escaping Quotes

In bash you are able to escape only double-quotes inside double quotes.

Example (This is okay):

 

You CAN’T escape single quotes inside single quotes

Example (This is NOT):

 

 

REFERENCES

One of the best places to learn BASH for me is TLDP:

http://tldp.org/LDP/abs/html/abs-guide.html

bugbear

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha * Time limit is exhausted. Please reload CAPTCHA.