In this post I wanted to go over some basic Bash Scripting. Once you know the basics of bash for navigating file systems and executing commands learning how to write basic scripts can be helpful as well. For instance with bash scripting you can regularly move files from one system to another, rename a large number of files and folders, or perform a certain set of operations every time a system boots up. I’ll cover Variables, If statements, and Loops.
Getting started:
Using your Bash terminal find a nice directory to work in and enter the command touch myBashScript.sh
This will create a file with the conventional .sh
file extension. Open this file up in your favorite editor and add #!/bin/bash
on the very first line. This is called a shebang and it describes the interpreter that we’re going to use (in this case bash). Keep in mind that octothorps(#) can be used to write comments in your code.
Lets add a simple command to test this out. I’m gonna try this echo Greetings Planet
.
Now save your file, go back to the command line and type ./myBashScript.sh
. You will probably get a permissions related error. By default this file is not executable for security reasons. You can change this by using the chmod
command which can modify permissions. Try this chmod 755 myBashScript.sh
. Now you should be able to run ./myBashScript.sh
and get the expected results.
Variables:
Now that we know how to create a bash script we can really start programming. This starts with variables. Maybe you want your script to run with some arguments and do something with them. With bash scripts you have some special variables built in that reference arguments
$1 - $9
which reference each argument 1-9. In addition you can call $0
for the name of the script your running and $@
for all the arguments supplied. Try using these by editing your myBashScript.sh to look like this:
Save this file and got back to your command line and run the script again, but this time add some arguments like this ./myBashScript.sh cow chicken
. This should print “Greetings Planet cow chicken” to your command line. You can now combine this knowledge with your basic commands like cp
and mkdir
to build a simple backup script. If you want to define variables inside your script you can do so by adding a line like this myVariable=banana
If statements:
Bash also supports If statements. They are written like this:
if [ $1 -lt 5 ]
then
echo argument 1 is less than 5
fi
The portion in the brackets is a test that yields true or false. Here I am testing whether the first argument($1
) is less than(-lt
) 5. You can also test if something is greater than -gt
, equal -eq
. If you want to test the equivalence of strings you can use =
and !=
between them. You can also use and -a
and or -o
between two expressions. A full list of tests can be found by typing man test
in your bash command line. Once you have your test you write then
, a series of commands, and then fi
to close the if statement. Try the code above and see how it goes.
Loops:
Once you understand the structure of If statements Loops should come naturally. There are a few types of loops but I’ll cover 2 common ones While and For.
A while loop is structured like this:
myVariable=0
# this should print numbers 0-4
while [ $myVariable -lt 5 ]
do
echo $myVariable
((myVariable++))
done
As you can see this is very similar to an If statement except you replace the if
, then
and fi
with while
, do
and done
. Try running this code and make sure you get number 0 – 4 printing to the terminal. You’ll noticed is used double parentheses with my variable name and ++ to increment the variable as we go through the loop. You can nest if statements in here and us terms like break
and continue
for more control over the looping.
Unsurprisingly a For loop is similar to a While loop. The syntax for a For loop looks like this:
myList='sleepy awake tired'
# this should print sleepy awake tired on separate lines
for item in $myList
do
echo $item
done
As you can see we removed the test condition and replaced it with an expression that pulls each element out of a list and renders it as a normal variable in the loop item in $myList
. This can be done with a range as well by replacing the reference to $myList
with {1..5}
and the code should print 1-5 on separate lines in your command line. Try it out.
Tips:
- Be very careful with spaces, they are very meaningful to Bash. For example
{1..5}
is NOT the same as{ 1..5 }
- Remember to check out
man
for commands you want to learn more about - Bash was made for computers with very limited resources by modern standards, this means that the number of characters in a command was a significant issue. Most commands sound cryptic but if you learn what they stand for make a lot more sense and help with the learning curve. cp -> copy, man -> manual, mkdir -> make directory etc.
- Look into cron jobs and crontab to learn how to automate tasks or run scripts when your system boot up.
To learn more check out ryanstutorials.net this series goes more in depth and gave me inspiration for examples and topics to cover.