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: |
#, 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.
$> MYVAR="foo.bar"
$> echo ${MYVAR#foo}
.bar
$> echo ${MYVAR%bar}
foo.
$>
$> 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)
-
letinternal 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)
$> declare -a tab
$> tab[0]="foo"
$> echo ${tab[0]}
foo
$>
$> 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-
elifandelseavailable if needed
-
-
- Loops
-
-
while <command list> ; do <command list> ; done -
until <command list> ; do <command list> ; done
(it is actually awhilewith reverse condition) -
for <var> in <list> ; do <command list> ; done(iterator)
-
Control structures
- Comparison
-
-
AandBare string arguments:
-n A,-z A,A == B,A != B,A < B,A > B -
MandNare 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 … ]] -
OPmay 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
returnkeyword⇒ 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
functionkeyword may be omitted -
a
nameis mandatoryfunction 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
-
"Unix Text Processing", Dale Dougherty and Tim O’Reilly, Hayden Books, 1987
https://www.oreilly.com/openbook/utp/ -
Christophe Blaess cheat sheets[FR]
https://www.blaess.fr/christophe/developpements/aides-memoires/-
Unix commands[FR]
https://www.blaess.fr/christophe/memo_commandes_unix.html -
Shell programming[FR]
https://www.blaess.fr/christophe/memo_programmation_shell.html
-
-
Rich’s sh (POSIX shell) tricks
https://www.etalabs.net/sh_tricks.html -
Bash Reference Manual
https://www.gnu.org/software/bash/manual/bashref.html
-
Advanced Bash-Scripting Guide
http://tldp.org/LDP/abs/html/ -
"Mastering Regular Expressions, 3rd Edition — Understand Your Data and Be More Productive", Jeffrey Friedl
https://www.oreilly.com/library/view/mastering-regular-expressions/0596528124/ -
"GAWK: Effective AWK Programming", Edition 4.1
http://www.gnu.org/software/gawk/manual -
Manual pages‡ :
bash(1),grep(1),regex(7),gawk(1)
‡ : read thoses pages on your own operating system, not on the Internet!