Write first bash script

After reading this short shell scripting tutorial, you will be able to to create and execute your own Bash shell script. No previous knowledge of shell scripting is required. However, you are expected to have some knowledge of how to start a command line terminal and how to edit text files with some text editor of your choice.

Before we dive directly into the Bash script, it is helpful to know how Bash works in the first place. We will explain this below, and you will learn about command output and interpreters on a Linux system. Follow along with our steps below to get started.

In this tutorial you will learn:

  • How to use Bash commands in terminal
  • How to indentify the path to your Bash shell interpreter
  • How to give a Bash script execute permissions
  • How to execute a Bash script in terminal
Write first bash script
Write first bash script
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software Bash shell (installed by default)
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Writing Your First Bash Shell Script




It is important to understand that writing a Bash script is very similar to simply entering commands in your terminal. As a matter of fact, the contents of a Bash script can be directly used in the terminal and will function the same. A Bash script is simply a series of commands that get executed in a predictable order.

With that said, for this first section, we are going to create a simple shell script that does nothing else but print “Shell Scripting” on the terminal output. This will will be your starting point. To put your first basic script together, you need to know two things:

How to execute command

Since the shell script is nothing more than just a collection of commands, before we can write any shell script we need to know how to execute commands in the terminal. Open your terminal and type echo Hello disregarding the $ sign at the beginning of the line, which only simply indicates that you are supposed to type commands as a non-privileged user:

$ echo Hello
Hello

In the above example, you have typed the echo command, which simply prints anything passed to it. In this case, we passed the string “Hello” and thus our string we printed is on the second line in the output shown above.

What Is an Interpreter?

The next thing you should keep in mind is that the Bash shell is an interpreter. What is an interpreter? In simple words, the interpreter is the process behind script execution capable of interpreting all commands within your shell.

Since we are writing a shell script, we can choose from a number of shells to interpret our commands available on the Linux system, however in our case we choose the default shell, Bash ( Bourne-again shell ). First, let’s locate our Bash shell interpreter using the which command to reveal a location of Bash interpreter on your system:

$ which bash
/usr/bin/bash

Now we are ready to write our first Bash shell script. Open your favorite text editor and enter the following code:



#!/usr/bin/bash 

echo "Shell Scripting"

Note that first line contains our interpreter which is indicated by the fact that is is located on the first line of our script, as well as that it is prefixed with #! (shebang) symbols. On the second line, we have typed our already known command echo, followed by a string to be printed out. Save this file in your home directory using name bash-script.sh.

How to Execute Shell Script

Now we are ready to execute our first script. There are two ways that we can execute the shell script from our command line. The first and the most simplest way is to prefix our new Bash script file name with the bash command. In this case, we do not need to give our file execute permissions first:

$ bash bash-script.sh
Shell Scripting

The other and more common way to execute a shell script is to make the script executable and simply execute it by specifying the full path of the script or prefix the script name with ./ if the script is located in you current working directory. First, make your script executable using chmod command:

$ chmod +x bash-script.sh

At this point, you are ready to execute you Bash script by specifying the full path to the script. For example:

$ /home/linuxconfig/bash-script.sh
Shell Scripting

Note that linuxconfig is the name of our home directory in this example.

Another way to execute our new Bash script is to prefix its file name with ./. Of course, this only works if your terminal’s present working directory matches that of the location of the script.

$ ./bash-script.sh
Shell Scripting




Congratulations, you have now successfully executed your first Bash script!

Closing Thoughts

In this tutorial, you learned how to create and execute your first Bash script on Linux. Since we assume this is one of your first interactions with the command line terminal, we also showed the steps to create the file, give it execute permissions, etc. Just in case you want a bit more of a challenge, we also have a tutorial on creating a Hello World Bash script in case you want to learn more.



Comments and Discussions
Linux Forum