June 2025
Beginner to intermediate
1180 pages
37h 48m
English
This section describes which bash keywords you can use to structure your code. You can create branches using if or case; loops using for, while, or until; functions using function; and so on.
As in almost any programming language, you can formulate branches using if. The syntax is as follows:
if condition1; then command1a command1b[ elif condition2; then commands2 ][ else commands3 ]fi
A concrete example looks as follows:
if [ $# -ne 2 ]; then echo "Two parameters must be passed to the command!" exit 2 # error code for wrong parameterselse echo "Parameter 1: $1, Parameter 2: $2"fi
The criterion for branching is the return value of the last command before then. The condition is met ...
Read now
Unlock full access