When using the Linux terminal, you may often find yourself needing to execute commands constantly and in the same order. Instead of manually typing these commands and opening them up to human error, we can instead place those commands in a file and execute them sequentially, saving us time and potential errors. What we will be using is called Scripting. This tutorial assumes that you have knowledge of basic shell commands like echo, mv, cd, touch, etc.
We will focus primarily on the Bourne shell, which is a command-line interpreter that accepts human input and converts it into language that the Unix kernel can understand. If you are familiar with programming, the Bash shell is similar to the Bourne shell but offers a bit more customization and similarities to other programming languages than the Bourne shell.
What is Scripting?
Scripting allows us to perform a group of commands with a single file execution. If you are familiar with programming languages, shell scripting should be relatively easy to pick up as it includes the use of variables and functions to help execute complex tasks. Learning the basics of scripting should make the quality of life when navigating the terminal much easier depending on your needs.
Writing our first script
First, we'll create a file called test.sh. We will then put the following as our first line of the file:
When executing our script, this line specifies the path to the CLI, or command-line interpreter, that we will be using. Next, we'll add the following to our file:
echo Hello World!
Save your file and exit the text editor. Scripts are not executable by default, so we must use chmod to modify user permissions like so:
chmod u+x test.sh
Finally, we can execute our script by running the following command:
If all went well, then you should see "Hello World!" printed on your terminal.
Congratulations! You've created your first shell script. Let's look at some more examples.
The following script will create a directory called "Example" in the present working directory while also creating 3 files in that directory. It will then print "Done!" once the files have been created.
mkdir Example #Making the directory
touch ./Example/test1.txt ./Example/test2.txt ./Example/test3.txt #creating the files inside the directory
echo "Done!" #Outputting done!
Using # as a precursor to text allows you to "comment", or write the text that will be ignored by the shell, allowing you to document your code.
Variables are essentially text that is assigned data values, and you can use these variables in your commands. For example, the following script outputs the value assigned to variable var1:
Shell scripts have environmental variables that are always present and track data. In particular, $1, $2, and $3 are environmental variables that store the 1st, 2nd, and 3rd command line arguments of the command, respectively. The following script shows how command-line arguments can be used:
echo Your first name is $1
echo Your last name is $2
echo Your age is $3
You should note that to run your scripts, you must always prepend your script with the directory path, such as ~/dir1/script.sh. If you would like to execute the script without prepending the directory path, as well as be able to execute the script from anywhere, you can input the following command:
This will allow you to run your script from any directory, as well as allowing you to execute script.sh without prepending a directory path.
That's it for this introduction to scripting. Shell scripting has much potential for quality of life and automation of daily tasks, so if you would like to make your journey around the Linux terminal more interesting, delving into scripting will yield good results.