A command interpreter, a programming language

  • Bash is a command interpreter

  • It also has a scripting language to achieve more complex tasks, as well as nice features to help you:

    • operations on variables

    • simple arithmetic

    • data structures (arrays)

    • tests, loops

    • functions

  • Can make a script executable using any command interpreter

Special variables

IFS

A character string containing all characters considered a word delimiter (default value: "<space><tab><new line>")

#, 1-9, @, *

number and list of positional parameters of a script or a function

0

name of the current script

?

exit status of the most recently executed command

$

PID of the current interpreter

!

PID of the job most recently placed into the background

More details in the bash(1) manual page, section PARAMETERS.

Variable processing

It is possible to achieve some simple operations on the values of the variables.

prefix and suffix removal
$> MYVAR="foo.bar"
$> echo ${MYVAR#foo}
.bar
$> echo ${MYVAR%bar}
foo.
$>
 
 
Pattern substitution
$> MYVAR="Gibi Byte"
$> echo ${MYVAR/i}
Gbi Byte
$> echo ${MYVAR//i}
Gb Byte
$> echo ${MYVAR//i/o}
Gobo Byte
$>

The value of the variable is not altered.

More details and more operations are available in the bash(1) manual page, section EXPANSION / Parameter Expansion.

Arithmetic

  • Simple arithmetic operations (integers only)

  • let internal command or the (( …​ )) construct

    $> a=1
    $> let "a=a+1"
    $> echo $a
    2
    $> ((a=a/3))
    $> echo $a
    0
    $>

Arrays

  • One-dimensional only

  • Considered as variables

  • Indexed or associative

  • Super verbose syntax ({} are mandatory)

Indexed
$> declare -a tab
$> tab[0]="foo"
$> echo ${tab[0]}
foo
$>
Associative
$> declare -A tab
$> tab["key"]="value"
$> echo ${tab["key"]}
value
$>

Control structures

Tests
  • Begin with [ or [[ (these are commands!)

Conditions
  • if <command list> ; then <command list> ; fi

    • elif and else available if needed

Loops
  • while <command list> ; do <command list> ; done

  • until <command list> ; do <command list> ; done
    (it is actually a while with reverse condition)

  • for <var> in <list> ; do <command list> ; done (iterator)

Control structures

Comparison
  • A and B are string arguments:
    -n A, -z A, A == B, A != B, A < B, A > B

  • M and N are integer arguments:
    M -eq N, M -ne N, M -lt N, M -le N, M -gt N, M -ge N

Logical operators
  • Used inside conditions: …​] OP […​
    or inside tests: [[ …​ OP …​ ]]

  • OP may be && (AND) or || (OR)

  • ! negates the value of the test

Functions

  • A named set of commands usable anywhere in a script, as often as necessary

  • May get positional parameters ($1 …​ $n)

  • May return an integer value (in the range [0..255]) with the return keyword

    ⇒ The return value is stored in the ? variable (echo $?)

  • May print data with echo, printf, …​

    ⇒ This is the de facto return mechanism res=$(hello Foo)

Functions

  • Declared with [function] <name>() {
    ends with }

    • the function keyword may be omitted

    • a name is mandatory

      function hello() {
        echo Hello ${1:-World}
      }

Functions

  • Called using the function name followed by possible parameters, without parentheses

    $> hello
    Hello World
    $> hello Bob
    Hello Bob
    $>

Creating an executable script

It is not entirely a Bash thing, but more precisely a kernel feature

The file must begin with a specific header know as shabang or shebang (#!) followed by the path to the command interpreter
⇒ Any command interpreter will do (bash, perl, python, ruby, …​)

#!/bin/bash
...
...

Creating an executable script

The file may be executable (x flag set), which is more convenient

$> ls -l my_script.sh
-rw-r--r-- 1 user user 1234 Jan   1 12:00 my_script.sh
$> chmod +x my_script.sh
$> ls -l my_script.sh
-rwxr-xr-x 1 user user 1234 Jan   1 12:00 my_script.sh
$>

Debug?

As a debug mechanism, bash provides a way to print all actions taken during a script before taking them.
To do so, the x option must be set.

Enable debug
$> set -x
Disable debug
$> set +x

Beware of the high degree of verbosity your script may get!

References

‡ : read thoses pages on your own operating system, not on the Internet!