🐚Bash Lessons
Variables
Hiding Input from User
Conditionals
When bash scripting, you can use conditionals to control which set of commands within the script run. Use if
to start the conditional, followed by the condition in square brackets ([ ]
). Make sure you leave a space between a bracket and the conditional statement! then
begins the code that will run if the condition is met. else
begins the code that will run if the condition is not met. Lastly, the conditional is closed with a backwards if
, fi
.
A complete conditional in a bash script uses the following syntax:
Bash scripts use a specific list of operators for comparison. Here we used -lt
which is “less than”. The result of this conditional is that if $index
is less than 5, it will print to the screen. If it is 5 or greater, “5” will be printed to the screen.
Here is the list of comparison operators for numbers you can use within bash scripts:
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 comparing strings, it is best practice to put the variable into quotes ("
). This prevents errors if the variable is null or contains spaces. The common operators for comparing strings are:
Equal:
==
Not equal:
!=
For example, to compare if the variables foo
and bar
contain the same string:
Loops
There are 3 different ways to loop within a bash script: for
, while
and until
.
A for loop is used to iterate through a list and execute an action at each step. For example, if we had a list of words stored in a variable paragraph
, we could use the following syntax to print each one:
Note that word
is being “defined” at the top of the for loop so there is no $
prepended. Remember that we prepend the $
when accessing the value of the variable. So, when accessing the variable within the do
block, we use $word
as usual.
Within bash scripting until
and while
are very similar. while
loops keep looping while the provided condition is true whereas until
loops loop until the condition is true. Conditions are established the same way as they are within an if
block, between square brackets. If we want to print the index
variable as long as it is less than 5, we would use the following while
loop:
Note that arithmetic in bash scripting uses the $((...))
syntax and within the brackets the variable name is not prepended with a $
.
The same loop could also be written as an until
loop as follows:
Input
Within the script, these 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 print each color using:
Lastly, we can 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. For example, to get all files in a directory, you can use the *
character:
You can then iterate through each file and do something. Here, lets just print the full path and filename:
Functions
Last updated