Bash Cheatsheet
Multi-line Command
To execute multi-line command in bash script simple put a \ after breaking up your commands. There should be no white spaces after the backslash, else it will fail!
Every single line should be followed by a \ until you finished typing the command.
curl https://cat.png \#no spaces after!
-o here.txt \
If-Statements
In Bash the if-statements follows the structure:
if CONDITION
then
COMMAND
elif CONDITION
then
COMMAND
else
COMMAND
fi
As you can see every if chain will end with fi keyword. Every if/elif condition statement will be followed by the then keyword.
If you want to put the then on the same line with the if keyword, and because if, then, else, elif, fi are all shell keyword they cannot be used on the same-line. To fix this you have to put a ; to end the previous statement and the keyword before you can use another keyword. As an example:
if CONDITION; then
COMMAND
fi
Conditions
Okay, now we know the basic of if-statements how do I put it into use by filling in the conditions? There are couple different ways of writing conditions, here I will only go over the most commonly used ones.
1.
# The first way is using test
if test <expressions>; then
COMMAND
fi
Using this method you can test whether the files exists, and compare values. It has it's own set of syntax for example to check if a certain file named "foo.txt" exists then you would type test -f "foo.txt"
and it will evaluate to true if only the file "foo.txt" exists.
For comparing values we cannot use the symbols directly, ==, <, >, <=, or >=. Instead we have to use their flag equivalent below:
Compartor |
Flag Equivalent |
== |
-eq |
> |
-gt |
< |
-lt |
>= |
-ge |
<= |
-le |
or |
|| |
and |
&& |
not |
! |
You can also append ! to negate the expression to check the opposite. Use = to do string equality comparison.
2.
# The second way is using [] square brackets
if [ some test ]; then
COMMAND
fi
The square bracket is like test and essentially all the operators that you can use with test you can also use in the square brackets.
There must be a space between the test and the left bracket, [ and the right bracket, ], otherwise Bash cannot understand it!
3.
# The third way is using [[]] double square brackets
if [[ some test ]]; then
COMMAND
fi
The double square brackets is like an upgrade of the normal square bracket. It comes from ksh.
With the double square brackets you can use some of the comparison operators without using the flags. So we are allowed to use >, =, and < without using the flag equivalent. However, <= and >= still requires the flag equivalent.
4.
if command;then
COMMAND
fi
Bash runs the command you have provided and then will run the if-statement according to the exit code. It will run it if the exit code is 0, and will not run it if it is not 0.
Remember in programming, 0 represent the command carried out successfully, and anything not 0 represent some sort of errors occurred.
Variables & Arrays
To declare a variable in Bash follow the following syntax structure:
VAR_NAME=VALUE
You must not use any spaces between the variable name and the value! Otherwise, it will error out because you are not following Bash syntax!
Command Output -> Variable
To store the output of a command into a variable follow the following syntax structure
VAR_NAME=$(COMMAND ARGS)
OR
VAR_NAME=`COMMAND ARGS`
This will do command substitution, it will execute the command and then substitute the return value as the value.
Arrays
To declare a array follow the following syntax structure:
arr=(1 2 3 4 5 6 7)
OR
arr=(
"hello"
"world"
"hehe"
"xd"
)
The array can contain different types, either an integer, float, or even strings.
You can refers to each individual elements using ${var_name[index]}
, the index are 1-based indices.
To refer to the entire array for say the usage of running a command with arguments from the entire array you can use $array_name[@]
to refer to all of the elements from the array.
Appending Element
To append to an array you would use the array_name+=(ele)
Empty Array
array_name=()
to create an empty array
Getting Length of Array
${!arr[@]}
retrieves the size of the array
Overwriting Array
arr[0]=3
will over the elements at index 0
For Loops
To do for loops there are manytwo variants,ways, element-wise and againindex I will only go over some of the basics ones.wise:
1. Looping through array elements
https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
for ele in ${array[@]}; do
echo $ele
done
2. Looping through array indices
for i in ${!array[@]}; do
echo $i
done