From charlesreid1

Revision as of 09:52, 12 April 2011 by Admin (talk | contribs)

Bash Guide

Using Bash

Setting the default shell: http://www.unix.com/shell-programming-scripting/32664-how-change-default-shell-linux.html


Startup Process

source /etc/profile

source ~/.profile

(source ~/.bash_profile?)

source /etc/bashrc

source ~/.bashrc


Dot Files

Bash Scripting

Looping

Looping in bash is really simple, and convenient - you can use other unix commands (most obviously "ls") to create lists, and then loop over each element of those lists. For example, if I want to print out the name of every file in my home directory (granted, not very useful, but this is just an example), I can do this:

for i in `/bin/ls -1 $HOME`; do
    echo $i
done

To do some serious bash looping kung-fu, check out Xargs.

Logical Operators

NOTE: the use of double brackets for logical condition checks is highly recommended.

There are several logical operators available for checking conditions in bash.

These can be tested using a simple "if" statement. In bash, if statements are of the form:

if [[ condition ]]; then
    cmd
else
    cmd2
fi

The spaces between the brackets and the condition are essential.

Integer Operators

Operator Meaning Example
-eq returns true if arguments are equal
$ a=1; b=1; if [[ "$a" -eq "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=1; b=2; if [[ "$a" -eq "$b" ]]; then echo "true"; else echo "false"; fi
false
-ne returns true if arguments are not equal
$ a=1; b=1; if [[ "$a" -ne "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=1; b=2; if [[ "$a" -ne "$b" ]]; then echo "true"; else echo "false"; fi
true
-gt returns true if argument 1 is greater than argument 2
$ a=5; b=10; if [[ "$a" -gt "$b" ]]; then echo "true"; else echo "false"; fi
false
-ge returns true if argument 1 is greater than or equal to argument 2
$ a=15; b=10; if [[ "$a" -ge "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=10; b=10; if [[ "$a" -ge "$b" ]]; then echo "true"; else echo "false"; fi
true
-lt returns true if argument 1 less than argument 2
$ a=5; b=10; if [[ "$a" -lt "$b" ]]; then echo "true"; else echo "false"; fi
true
-le returns true if argument 1 less than or equal to argument 2
$ a=15; b=10; if [[ "$a" -le "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=10; b=10; if [[ "$a" -le "$b" ]]; then echo "true"; else echo "false"; fi
true
\< returns true if argument 1 less than argument 2

this needs to be escaped with a \ if it occurs inside single brackets; otherwise it can appear between double bracket or between double parentheses

$ a=10; b=10; if [ "$a" \< "$b" ]; then echo "true"; else echo "false"; fi
false

$ a=10; b=10; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=10; b=10; if (( "$a" < "$b" )); then echo "true"; else echo "false"; fi
false
<= returns true if argument 1 less than or equal to argument 2

this operator needs to go inside double parentheses

$ a=10; b=10; if (( "$a" <= "$b" )); then echo "true"; else echo "false"; fi
true
> returns true if argument 1 greater than argument 2

this needs to be escaped with a \ if it occurs inside single brackets; otherwise it can appear between double bracket or between double parentheses

$ z=10; zz=1000; 

$ if [ zz \> z ]; then echo "true"; else echo "false"; fi
true

$ if [[ zz > z ]]; then echo "true"; else echo "false"; fi
true
>= returns true if argument 1 greater than or equal to argument 2

this operator needs to go inside double parentheses

$ a=50; b=10; if (( $a >= $b )); then echo "true"; else echo "false"; fi
true

String Operators

Operator Meaning Example
= returns true if two strings are equal
$ a=foo; b=foo; if [[ "$a" = "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=foo; b=bar; if [[ "$a" = "$b" ]]; then echo "true"; else echo "false"; fi
false
== returns true if two strings are equal; equivalent to =
$ a=foo; b=foo; if [[ "$a" == "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=foo; b=bar; if [[ "$a" == "$b" ]]; then echo "true"; else echo "false"; fi
false

Note the difference between quoting a string and not quoting a string:

[[ $a ==  b*  ]] # returns true if $a starts with b (pattern matching)
[[ $a == "b*" ]] # returns true if $a is literally equal to b* (literal matching)
!= returns true if strings are not equal
$ a=foo; b=bar; if [[ "$a" != "$b" ]]; then echo "true"; else echo "false"; fi
true
< returns true if argument 1 is less than (alphabetically) argument 2; capitals come before non-capitals
$ a=foo; b=bar; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
false

$ a=Foo; b=bar; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=bar; b=foo; if [[ "$a" < "$b" ]]; then echo "true"; else echo "false"; fi
true
> returns true if argument 1 is greater than (alphabetically) argument 2; capitals come before non-capitals
$ a=foo; b=bar; if [[ "$a" > "$b" ]]; then echo "true"; else echo "false"; fi
true

$ a=Foo; b=bar; if [[ "$a" > "$b" ]]; then echo "true"; else echo "false"; fi
false
-z returns true if the string is null
$ a=""; b="foobar"; if [ -z "$a" ]; then echo "true"; else echo "false"; fi
true

$ a=""; b="foobar"; if [ -z "$b" ]; then echo "true"; else echo "false"; fi
false
-n returns true if the string is NOT null
$ a=""; b="foobar"; if [ -n "$a" ]; then echo "true"; else echo "false"; fi
false

$ a=""; b="foobar"; if [ -n "$b" ]; then echo "true"; else echo "false"; fi
true


Compound Operators

You can



      ! ~             logical and bitwise negation
      <>       left and right bitwise shifts
      =
                       comparison
      == !=       equality and inequality
      &               bitwise AND
      ^               bitwise exclusive OR
      |                bitwise OR
      &&             logical AND
      ||              logical OR
      expr?expr:expr
                       conditional operator
      = *= /= %= += -= <>= &= ^= |=
                       assignment
      expr1 , expr2
                       comma

Basic Math Operators

You can do some basic integer math using bash as follows:

Addition

$ sum=$((2+2))  &&   echo $sum
4

$ sum=$((sum+10))  &&   echo $sum
14

Subtraction

$ diff=$((100-50))   &&   echo $diff
50

$ diff=$((diff-25))   &&   echo $diff
25

Multiplication

$ prod=$((2*8))    &&   echo $prod
16

$ a=9; b=3; prod=$((a*b))    &&   echo $prod
27

Division

$ a=14; b=2; div=$((a/b))   &&   echo $div
7

Remainder

$ a=10; b=4; z=$((a%b))   &&   echo $z
2

Exponentiation

$ z=5   &&   echo $z
5

$ zz=$((z**2))   &&   echo $zz
25

NOTE: Bash does integer math only! To do fraction math, use the program bc or awk.

Complex Math Operators

You can use the increment and decrement operators ++ and -- as follow:

Increment++

The increment operator can be used as either a prefix or postfix operator, i.e. as ++i or i++. Example:

$ z=1   &&   echo $z
1

$ ((++z))   &&   echo $z
2

$ ((++z))   &&   echo $z
3

$ ((++z))   &&   echo $z
4

This can be used in bash for loops as follows:

for ((i=1; i <=$NUM ; i++)); do
    echo $i
done

Decrement--

Like the increment operator, this can be a prefix or postfix operator, i.e. --i or i--:

$ z=1   &&   echo $z
1

$ ((--z))   &&   echo $z

$ ((--z))   &&   echo $z
-1

$ ((--z))   &&   echo $z
-2

$ ((--z))   &&   echo $z
-3

Notice that echo $z doesn't print anything if z=0.

Bash Keyboard Shortcuts

Editing Shortcuts

The following are keyboard shortcuts for easy navigation/editing of commands that are on the Bash command line.

Movement

  • C-a - Move to beginning of line
  • C-e - Move to end of line


  • M-f - Move forward 1 word
  • M-b - Move backward 1 word


  • C-f - Move forward 1 character
  • C-b - Move backward 1 character

Cutting and Pasting

  • C-k - kill (cut) from cursor to end of line
  • C-w - kill (cut) from cursor to previous whitespace
  • C-y - yank (paste) previously killed text at cursor

Uncategorized

  • C-L - clear screen, reprint current line at top
  • C-u - undo last edit
  • C-d - delete character under cursor

References

Floating point math in the shell:

http://www.novell.com/coolsolutions/tools/17043.html

Fast bash math:

http://www.bytemycode.com/snippets/snippet/350/

Bash script iterate through array of values

http://www.tech-recipes.com/rx/636/bash-shell-script-iterate-through-array-values/