Bash Scripting Cheatsheet
Bash Scripting Cheatsheet
When saving the script, it’s good practice to place commonly used scripts in the ~/bin/
directory.
A script file also needs to have the “execute” permission. To do this to a file script.sh ,
you use:
chmod +x script.sh
Every time you open a terminal, it loads its configurations from ~/.bashrc ( ~/.bash_profile
on OSX). To ensure that scripts in ~/bin/ are available, you must add this directory to
your PATH
• PATH=~/bin:$PATH
Variables
In a script (or the terminal), you can set a variable by providing a name and a value in
the following way:
greeting="Hello"
To use the value of a variable, we type the variable name prepended by a dollar sign $ .
If we wanted to print out the greeting defined above, we would use:
echo $greeting
Conditionals
Conditionals allow us to control the code that will run based on a certain condition. To
start an if statement, we use if and then follow it with square brackets [] . Inside those
square brackets we will write our condition, as per the following: [ condition ] (note: the
space between brackets and condition must be kept). After that, we write then and
define the code that will run if our condition is met. After that, we can add an else
statement that will run if our condition is not met. Lastly, we close an if with the line
fi (reversed if).
if [ $index -lt 5 ]
then
echo $index
else
echo 5
fi
• Equal: -eq
• Is null: -z
When it comes to strings, best practice dictates that we put variable names in between
quotes. This is done to prevent errors if the variable is null or contains spaces. The
operators for comparing strings are:
• Equal: ==
• Not equal: !=
if [ "$foo" == "$bar" ]
Loops
There are three ways to loop in a bash script: for , while and until .
A for loop is used to iterate through a list and perform an action at each step. If we had
a list of words stored in a variable $paragraph , we could print each word by using a for
loop:
Also note that in the last code snippet, word is being “defined” at the top of the for loop.
No $ prepended. When in the do block, we do prepend a $ to access the variable as
per usual.
The until and while loops are very similar within bash scripting. The while loop will run
as long as the provided condition is true whereas until will loop until the condition is
true. In other words, while will iterate as long as the condition evaluates to true and the
until loop will iterate as long as the condition evaluates to false . Conditions are
Note that arithmetic in bash uses the $((…)) syntax and within the brackets the variable
is not prepended with a $ .
Inputs
We can prompt the use for input by using the read syntax. E.g., to ask the user for input
and save it to the number variable, we would use the following code:
Another way to access external data is to have the user add input arguments when they
run the script. The arguments are entered after the script name and separated by
spaces.
Within a script, these arguments are accessed using $1 , $2 , etc… Where $1 is the first
argument (here, red) and so on. Note that these are 1 indexed.
If your script needs to accept an indefinite number of input arguments, you can iterate
over them using the "$@" syntax.
files=/some/directory/*
Aliases
You can set up aliases within the .bashrc or .bash_profile files to allow calling your scripts
without the full filename. E.g., if we had a saycolors.sh script, we could alias it to the
word saycolors using the following sintax:
alias saycolors='./saycolors.sh'
And we could even add standard input arguments. If we always want “green” to be
included as first input to saycolors , we could modify the alias:
https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dbb1e262-22da-
49ae-97e5-92044f65937f/CC_build_project.sh