Bash Shell Scripting Definition
- Bash is a command language interpreter. It is widely available on various operating systems and is a default command interpreter on most GNU/Linux systems. The name is an acronym for the ‘Bourne-Again SHell’.
- Shell is a macro processor which allows for an interactive or non-interactive command execution.
- Scripting allows for an automatic commands execution that would otherwise be executed interactively one-by-one.
Bash Shell Script Basics
What is Shell
Most likely, your are at the moment sitting in front of your computer, have a terminal window opened and wondering: “What should I do with this thing?”
Well, the terminal window in front of you contains shell, and shell allows you by use of commands to interact with your computer, hence retrieve or store data, process information and various other simple or even extremely complex tasks.
Try it now! Use your keyboard and type some commands such as
ls followed by the
What you have just done, was that by use of commands and shell you interacted with your computer to retrieve a current date and time (
date), looked up a calendar (
cal), checked the location of your current working directory (
pwd) and retrieved a list of all files and directories located within (
What is Scripting
Now, imagine that the execution of all the above commands is your daily task. Every day you are required to execute all of the above commands without fail as well as store the observed information. Soon enough this will become an extremely tedious task destined for failure. Thus the obvious notion is to think of some way to execute all given commands together. This is where scripting becomes your salvation.
To see what is meant by scripting, use shell in combination with your favorite text editor eg. vi to create a new file called
task.sh containing all the above commands, each on a separate line. Once ready, make your new file executable using
chmod command with an option
+x. Lastly, execute your new script by prefixing its name with
As you can see, by use of scripting, any shell interaction can be automated and scripted. Furthermore, it is now possible to automatically execute our new shell script
task.sh daily at any given time by use of cron time-based job scheduler and store the script’s output to a file every time it is executed. However, this is a tale for an another day, for now let’s just concentrate on a task ahead.
What is Bash
So far we have covered shell and scripting. What about Bash? Where does the bash fit in? As already mentioned, the bash is a default interpreter on many GNU/Linux systems, thus we have been using it even without realising. This is why our previous shell script works even without us defining bash as an interpreter. To see what is your default interpreter execute command
$ echo $SHELL /bin/bash
There are various other shell interpreters available, such as Korn shell, C shell and more. From this reason, it is a good practice to define the shell interpreter to be used explicitly to interpret the script’s content.
To define your script’s interpreter as Bash, first locate a full path to its executable binary using
which command, prefix it with a shebang
#! and insert it as the first line of your script. There are various other techniques how to define shell interpreter, but this is a solid start.
Hello World Bash Shell Script
Now, it is time to write our first, most basic bash shell script. The whole purpose of this script is nothing else but print “Hello World” using
echo command to the terminal output. Using any text editor create a new file named
hello-world.sh containing the below code:
#!/bin/bash echo "Hello World"
Once ready, make your script executable with the
chmod command and execute it using relative path
$ chmod +x hello-world.sh $ linuxconfig.org:~$ ./hello-world.sh Hello World $
The following video example offers an alternative way of creating the above
hello-world.sh script. It uses
which command to print a full path to the bash interpreter. This output is simultaneously redirected using
> redirection sign while creating a new file
hello-world.sh at the same time.
Simple Backup Bash Shell Script
Let’s discuss a command line execution and how GNU/Linux commands fit into the shell script creation process in more detail.
Any command which can be successfully executed directly via bash shell terminal can be in the same form used as part of bash shell script. In fact, there is no difference between command execution directly via terminal or within a shell script apart from the fact that the shell script offers non-interactive execution of multiple commands as a single process.
Additionally, most commands accept so called options and arguments. Command options are used to modify command’s behaviour to produce alternative output results and are prefixed by
-. Arguments may specify command’s execution target such as file, directory, text and more.
Each command comes with a manual page which can be used to learn about its function as well as what options and arguments each specific command accepts.
man command to display manual page of any desired command. For example to display a manual page for the
ls command execute
man ls. To quit from manual page press
ls command example shows a basic use of command line options and arguments.
Although our first “Hello World” shell script requires a solid understanding of the file creation, editing and script execution, its usability can be clearly questioned.
The next example offers more practical application as it can be used to backup our user home directory. To create the backup script, on Line 3we will be using
tar command with various options
-czf in order to create a compressed tar ball of entire user home directory
/home/linuxconfig/. Insert the following code into a new file called
backup.sh, make the script executable and run it:
#!/bin/bash tar -czf /tmp/myhome_directory.tar.gz /home/linuxconfig