How to Create Shell Scripts in Linux/Unix

In simple terms, shell script in Linux/Unix is a group of commands you execute in your shell. Just copy and paste your commands into a file and your shell script is prepared. Shell scripts can alleviate your need for many software. Using shell scripts you can automate most of your tasks.

In this article, I will talk about Posix shells – mainly bash shell. Most of these commands will also work in other POSIX-compliant shells such as Zsh and Dash. First, I will tell you about basic commands like how to set and get variables and their values. Then, I will talk about how to create and run shell scripts.

📝 Note: If you have knowledge of any programming language, it will be easier to understand bash shell. But I will describe each step, so don’t worry.

Table of Contents

Variables and basic commands in bash shell in Linux

Open your terminal, launch the bash shell, and learn these basic commands first.

How to define variables in bash shell

To define a variable and its value, you just need to follow the simple method:

[ajay@legion ~]$ variable=value

Where, the value can be an integer, floating point number (i.e. a number with decimal), or strings (i.e. texts)

Examples:

Number:

[ajay@legion ~]$ x=3

String:

[ajay@legion ~]$ y="item_number_one"

Please note that there is no space around the equal = sign.

It is good to always use quotes around your values, otherwise, you might get errors caused by “splitting”, or “substitution”. They are not errors per se. These splitting and substitution have a special application. I will talk about them in later articles.

[ajay@legion ~]$ variable='United States'

How to get the value of any variable in the bash shell in Linux

To get the values of these variables, you need to prepend $ to these variable names. For example, x becomes $x, y becomes $y and so on. Now, I will be using echo to give you simple examples. To know more about echo, look at my article on it.

Example:

[ajay@legion ~]$ echo "this is value of x: $x"

Output:

this is value of x: 3

Notice how the $x has been substituted by its value.

If you don’t give any value to any variable, you will get null result. For example, for an undefined variable z, I get the following result:

[ajay@legion ~]$ echo "this is value of z: $z"

this is value of z:

Variable name comprises:

  1. Word constituent character (\w): numbers, upper and lowercase alphabets, and underscore ( _ )
  2. It should not start with a number.

For example, variable_name, variable2 is allowed but 2variable is not allowed.

To learn more about bash shell in Linux, look at this article.

How to prepare shell scripts in Linux/Unix

Step 1: Create a file with the first line specifying the shell

First, open a file called ~/example.sh using your favorite text editor.

[ajay@legion ~]$ gedit ~/example.sh

The first line should tell which shell to be used. For example, to use bash shell, use the following in the first line:

#!/bin/bash

zsh shell

#!/bin/zsh

dash shell,

#!/bin/dash

Please note that /bin/bash is the path to bash binary. You can use any binary like that of python (/bin/python), gawk (/bin/gawk), etc. If your shell binaries are located in other paths, use them instead. (For example, #!/usr/bin/bash)

Step 2: Put all shell commands in the file

Now, whatever commands you want to run, put them into the file. For examples given above, I will be writing the following commands in the file:

#!/bin/bash
x=3
echo "this is value of x: $x"
echo "this is value of z: $z"

Step 3: Set the appropriate execution bit on the file

You might already know that files have permissions like read (r) and write (w). If a file does not have write permission, it means you cannot write anything into it.

Similarly, there is another permission called ‘execute’ permission (x). In order to ‘run’ any file, you need to set this permission. Linux, by default, removes this permission on all newly created files. This is a security feature in Linux. So, you need to set the execution bit (i.e. ‘execute’ permission). Use the following command to set the execution bit:

[ajay@legion ~]$ chmod +x your_file_name

So for the file name example.sh, the command will be:

[ajay@legion ~]$ chmod +x example.sh

Now, your shell script is prepared. You can run it now.

How to run shell scripts in Linux/Unix

There are many methods to run shell scripts. Two methods are:

  1. Run the script using any shell
[ajay@legion ~]$ bash file_name

Or,

[ajay@legion ~]$ sh file_name

Or,

[ajay@legion ~]$ zsh file_name

2. To run any shell script go to the folder in which it is located on your terminal type ./file_name and then hit the Enter button.

For example, to run the above shell script, ~/example.sh. First, open your terminal, go to the home directory using the cd command:

[ajay@legion ~]$ cd ~ 

Now, run the command:

[ajay@legion ~]$ ./example.sh

Going to the script’s directory each time we want to run the script is a cumbersome process. To remove this much work, you can include the script’s directory in the variable PATH.

PATH variable in Linux

PATH variable sets a group of directories separated by a colon (:). If you run your script from your terminal (or anywhere), the Linux system will look into these directories and try to find the file example.sh. If it cannot find the file, it will throw an error.

The default value of the PATH variable in your PC can be found using echo:

[ajay@legion ~]$ echo "$PATH"
/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/ajay/.local/share/npm/bin

So, if you want your computer to find your script, you need to put it in one of these directories. My recommendation will be to create a special directory for your personal scripts. I use ~/.my_scripts:

[ajay@legion ~]$ mkdir ~/.my_scripts

Now, move all of your personal script files into this directory. Then add this directory into the PATH using export:

export PATH=${PATH}:~/.my_scripts/

However, the above command is just a temporary addition. When you log out from your system, this path is excluded automatically. To add permanently, open the file ~/.profile and/or ~/.zshrc using your favorite text editor and add the above export line in the end.

Now, you can run your script easily. Just type the file name and hit enter.

[ajay@legion ~]$ file_name

For example, if we want to execute the above script example.sh:

[ajay@legion ~]$ example.sh

Output:

this is value of x: 3
this is value of z: 

Now, you can bind this script to a shortcut key. Then you will never need a terminal to execute it.

Conclusion

That’s all. This was a basic introduction to shell scripting. Now, you need to learn about Linux command line tools:

If you have queries or suggestions, put them in the comment section below.

2 thoughts on “How to Create Shell Scripts in Linux/Unix”

  1. Pingback: Understanding Brace Expansion in Bash: Generating Sequences | SmartTech101

  2. Pingback: How to Use Conditional Statements in Bash: The If-Else Fi Basics | SmartTech101

Leave a Comment

Your email address will not be published. Required fields are marked *