BASH [http://www.gnu.org/software/bash/ (Bourne Again Shell)] is a command shell and interpretor for Linux.

==Parameters== $# Number of parameters

$1 First parameter

$0 Name of the script

$@ All the parameters in one string

==Variable Expansion== *Remove matching suffix: ${var%suffix} *Find and replace: ${var/test1/test2}

==Get IP address==

The following command will print the IP address by itself allowing you to pipe it into a command or an environment variable:

ifconfig|grep "inet addr:"|grep -v "127.0.0.1"|cut -d: -f2|awk '{ print $1}'

==Loops== ===Iterate through words in a string===

users="tom jerry ben"
for character in $users
do
  echo $character is a good name
done

===Looping through a series of numbers===

for i in `seq 1 5`;
do
  echo $i
done

==If==

oldusers="tom jerry sam micky minnie"
if [ ! -d /var/spool/mail ]; then
        echo /var/spool/mail not found - check location
else
        echo Checking /var/spool/mail
        for user in $oldusers
        do
        if [ -e /var/spool/mail/$user ]; then
                echo /var/spool/mail/$user exists
        fi
fi

===Numeric Operators=== ====Single Braces==== {| | -eq||equal |- | -ne||not equal |- | -gt||greater than |- | -ge||greater than or equal to |- | -lt||less than |- | -le||less than or equal to |} ====Double Parentheses==== Standard mathematical operators apply {| | <=||less than or equal to |- | >||greater than |- | >=||greater than or equal to |} ===String Operators=== ====Single Braces==== {| | =||equal |- | ==||equal, but has file globbing and word splitting when wildcarded without quotes, and literal matching with quotes |- | !=||not equal |- | -z||string is null |- | -n||string is not null - MUST be quoted string but string should always be quoted anyway… |} ====Double Braces==== {| | <||less than in ASCII order |- | >||more than in ASCII order |- | ==||equal, but has pattern matching when wildcarded, literal matching with quotes |} ===File Operators=== ====Single Braces==== *Unary tests: {| | -d||Directory |- | -e OR -a||Exists |- | -f||Regular file |- | -h OR -L||Symbolic link |- | -p||Named pipe |- | -r||Readable by you |- | -s||Not empty |- | -S||Socket |- | -w||Writable by you |- | -N||Has been modified since last being read |- | |} *Compare two files: {| | -nt||file1 is newer than file 2. File modification time stamp is used. |- | -ot||file1 is older than file 2. File modification time stamp is used. |- | - ef||file1 is a hard link to file2. |- | |}

===Test if number===

[[ "$var" =~ ^[0-9]+([.][0-9]+)?$ ]] && echo $var is a number

==Force run as root==

if [ $UID != "0" ];then echo Only root can run this command 1>&2;exit 1;fi

==Favorite Resources== [http://books.google.co.uk/books?hl=en&id=OztsPBFGhDIC&dq=BASH&printsec=frontcover&source=web&ots=p_MixNXpzD&sig=9y_nSRxEbj9jU4HJUMsAO7l-3Y0 BASH for beginners book]
[http://www.thelinuxblog.com/bash-scripting-techniques/ BASH Scripting Techniques]
[http://steve-parker.org/sh/buy/shellscriptingbook-sample.pdf Steve Parker’s Shell Scrpting Book]
[http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html If]
[http://tldp.org/LDP/abs/html/refcards.html Operators and constructs]

[[Category:Linux]][[Category:Bash]]