Advanced Bash
Author(s): Matthias Lee (ml2322)
Last Updated: 07-07-2025
Recommended Prerequisites (click to expand)
In addition to what we've already learned, bash has some very useful advanced functions.
Exit codes
After a command completes in bash, the ? variable will be set to its exit code.
user@system:/home/user$ ls
Desktop
Documents
Downloads
user@system:/home/user$ echo $?
0
user@system:/home/user$ ls Images
ls: cannot access 'images': No such file or directory
user@system:/home/user$ echo $?
2
Interpolation
Just like Bash can substitute variable names for there values, it can also replace commands with what happens when you run them with interpolation.
If you wrap a command in $() or ``, it will be executed, and replaced with its result. For example, if a_command would return Desktop,
ls $(a_command) will actually execute as ls Desktop, and will therefore list everything in the desktop. $() and `` are equivalent in
function, but the former is preferred in most cases, as it is cleaner and can nest.
Arrays
Bash can hold not just text, but arrays, in its variables. You define an array in parenthesis like this: array=(one two three). To access elements in
an array, we use the ${} syntax from earlier like this: ${array[0]}. Like any regular language, 0 is the start, and -1 is the end. ${array[@]}
returns every element in the array. You can also use slices like this: ${array[@]:0:3}, which will get elements 0, 1, and 2. To take slices from the
end, you can do ${array[@]: -2}, which will get the last two elements. NOTE: You do need to include the space after the colon for this.
user@system:/home/user$ array=(one two three four)
user@system:/home/user$ echo ${array[0]}
one
user@system:/home/user$ echo ${array[2]}
three
user@system:/home/user$ echo ${array[-1]}
four
user@system:/home/user$ echo ${array[@]}
one two three four
user@system:/home/user$ echo ${array[@]:0:3}
one two three
user@system:/home/user$ echo ${array[@]: -3}
two three four
Bash for scripting
Bash, as previously mentioned, can be used for scripts too, so it includes all the control features you would expect of a scripting language.
IF statements
Bash IF statements use the following syntax:
if <commands>; then
...
elif <commands>; then
...
else
...
fi
IF statements are decided based on the commands you put in <commands> (you can use command joining). It will execute if the commands
succeed (exit code 0). However, to do more basic operations like comparing strings in an if statement, you can use [[ ]], and (( ))
for math. Technically, [[ is a command, but we can treat it as just part of the syntax. Some basic ways to use this are as follows:
if [[ "str a" == "str b" ]]; then
(runs if the two strings are equal)
if [[ "str a" != "str b" ]]; then
(runs if the two strings are not euqal)
if (( 1 == 2 )); then
(runs if the two operands are equal)
if (( 1 > 2 )); then
(runs if 1 > 2)
if (( $a <= $b )); then
(runs if a <= b)
NOTE: The strings can include variables, or even include just a variable, and the numbers can be substituted for variables that only contain numbers.
For loops
You can do a for loop in bash as follows:
for i in "${array[@]}"; do
echo "$i"
done
This will iterate over every element in array. You can use slicing here to only get some of the array.