Bash scripts can make things easier for you by automating whatever you tell them to do. They can be as simple or complex as you decide to make them. You are basically saving a list of commands in a file and when you execute this file, it does whatever your commands tell it to do. So let’s get to it.
Open a terminal and create a script file with the following command.
Giving the file the .sh extension lets your system know that this is a shell script. Now we need to add a line to this file that will tell your system to use the bash interpreter. This line is called a shebang and it will always be the first line in your bash scripts. Open the file with your preferred text editor, add the shebang to the first line of the script and save the file.
#!/bin/bash
Run this command in the terminal to make the file executable.
Your script is all set up now and we can begin adding commands. I figured we’d start where everyone starts, with the Hello World script. Copy the following into your script and save the file. When you run this script, it will print the words Hello World in your terminal.
#!/bin/bash echo "Hello World"
You can execute your script by typing ./ followed by the name of your script.
If the script is in a different directory, you can execute it by typing the full path to the script.
Next, we’ll see how you can store things in variables. In the example below, we are going to store the word World in a variable. After the variable has been declared, you can invoke it anytime by adding a $ in front of it. This example will also print the words Hello World just like the previous example.
#!/bin/bash myvar="World" echo "Hello $myvar"
If you were to use single quotes around Hello $myvar instead of double quotes, it will print exactly what is between the quotes and it will not invoke the variable. It will print Hello $myvar instead. This is handy information to keep in mind later on when you get some more complicated scripts going.
#!/bin/bash myvar="World" echo 'Hello $myvar'
Now let’s make it a little more interactive. The script below will ask you what your name is and then the read command will wait for your input. Whatever you enter will be saved in a variable and then it will print Hello, followed by the name you entered.
#!/bin/bash echo "What is your name?" read name echo "Hello $name"
You can add comments and notes to your script to remind you of what certain sections are doing. Just use the # at the start of the line.
#!/bin/bash echo "What is your name?" # The following command will wait for your input. read name echo "Hello $name"
If Statements & Loops
The script below uses an if statement. This is a conditional statement. The script will ask you to enter your age. If you enter a number that is greater than 20, you will get the first result. Otherwise, you will get the second result.
#!/bin/bash echo "Enter your age." read age if [ "$age" -gt 20 ] then echo "You are old enough to drink." else echo "You are not old enough to drink." fi
Here is another example that asks you to choose a number between 1 and 100. If the number is greater than or equal to 51, you get the first message. If the number is equal to 50, you get the second message. Otherwise, you get the third message.
#!/bin/bash echo "On a scale of 1-100, how full is the glass?" read num if [ "$num" -ge 51 ] then echo "The glass is more than half full." elif [ "$num" -eq 50 ] then echo "The glass is half full." else
echo "You need a refill." fi
In this example, we will use a for loop to return all the strings in a variable. The variable in this example is a series of strings that are separated by spaces.
#!/bin/bash planets="Mars Jupiter Earth Saturn Neptune" for planet in $planets do echo "$planet" done
Output: Mars Jupiter Earth Saturn Neptune
Now we will loop through an array. Using @ in the variable below will print all the elements in the array. You can otherwise single them out by using a number. The first position is zero, not one. So Mario is 0. Luigi is 1. Peach is 2. And so on.
#!/bin/bash racers=('Mario' 'Luigi' 'Peach' 'Toad' 'Yoshi') for driver in "${racers[@]}"; do echo "$driver" done
Output: Mario Luigi Peach Toad Yoshi
This will loop over a number range. For each variable in the range between 5 and 1, it will echo the variable.
#!/bin/bash for i in {5..1} do echo "$i" done
Output: 5 4 3 2 1
This next loop uses three expressions. The first expression is where we initialize or set the variable.
i=5
The second expression is where we name our condition. If the variable is greater than or equal to 1, it will echo the variable as instructed. If the variable is not greater than or equal to 1, it will not echo the variable and the loop will stop.
i>=1
The third expression is the increment/decrement operation. In this example the variable will decrease by 1 until the condition in the second expression is no longer met.
i--
You can increment by using ++
instead.
#!/bin/bash for (( i=5; i>=1; i-- )) do echo "$i" done
Output: 5 4 3 2 1
If you leave the three expressions blank, it will create an infinite loop that will run forever until you stop it.
#!/bin/bash for (( ; ; )) do echo "Press CTRL+c to stop..." sleep 1 done
The break statement will terminate the current loop when a condition is met. In this example, the loop breaks when the color variable is Orange.
#!/bin/bash colors="Red Blue Yellow Orange Green" for color in $colors do if [[ "$color" == 'Orange' ]]; then break fi echo "$color" done
Output: Red Blue Yellow
The continue statement will pass control of the loop to the next iteration when a condition is met. In this example, when the color variable is Orange, it will jump to the next color in the list and continue on.
#!/bin/bash colors="Red Blue Yellow Orange Green" for color in $colors do if [[ "$color" == 'Orange' ]]; then continue fi echo "$color" done
Output: Red Blue Yellow Green
Hopefully you now have a better understanding of some of the basics of bash scripting. We’ve barely even scratched the surface on what types of things are possible, but for now I have to go.