0% found this document useful (0 votes)
72 views

Bash Scripting Cheatsheet

This document provides an introduction to Bash scripting. It covers important concepts like variables, conditionals, loops, inputs, and aliases. Variables store and retrieve values using a name and dollar sign. Conditionals like if/else statements control code execution based on conditions in brackets. Loops like for, while, and until iterate through lists. Inputs can come from user prompts, arguments when running a script, or external files. Aliases define shortcuts to call scripts.

Uploaded by

aaron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Bash Scripting Cheatsheet

This document provides an introduction to Bash scripting. It covers important concepts like variables, conditionals, loops, inputs, and aliases. Variables store and retrieve values using a name and dollar sign. Conditionals like if/else statements control code execution based on conditions in brackets. Loops like for, while, and until iterate through lists. Inputs can come from user prompts, arguments when running a script, or external files. Aliases define shortcuts to call scripts.

Uploaded by

aaron
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Learn Bash Scripting

Introduction to Bash Scripting


Variables
Conditionals
Loops
Inputs
Aliases

Introduction to Bash Scripting


There are conventions to follow to ensure that a Bash script will be executed. For
instance, the beginning of the script should start with #!/bin/bash on its own line. This
tells the computer which interpreter to use for the script.

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

Bash has a specific list of comparison operators:

• Equal: -eq

• Not equal: -ne

• Less than or equal: -le

• Less than: -lt

• Greater than or equal: -ge

• Greater than: -gt

• 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:

for word in $paragraph


do
echo $word
done

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

established the same way as an if loop.

while [ $index -lt 5 ]


do
echo $index
$index=$((index + 1))
done

Note that arithmetic in bash uses the $((…)) syntax and within the brackets the variable
is not prepended with a $ .

We could write the same loop with until :

until [ $index -eq 5 ]


do
echo $index
index=$((index + 1))
done

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:

echo "Guess a number"


read number
echo "You guessed $number"

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.

saycolors red green blue

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.

For our saycolors example, we could use:

for color in "$@"


do
echo $color
done
We can also access external files to our script. You can assign a set of files to a variable
name using standard bash pattern matching using regular expressions.

files=/some/directory/*

Then we can iterate the set:

for file in $files


do
echo $file
done

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:

alias saycolors='./saycolors.sh "green"'

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dbb1e262-22da-
49ae-97e5-92044f65937f/CC_build_project.sh

You might also like