The Bash and Bash Shell is the main management component for the Linux systems. Commands run on bash in order to complete tasks or jobs. These commands can be stored in a file which is called a shell script. The shell script is used to run single or more commands by just calling the shell script file without writing commands again and again. The shell scripts have the *.sh extension for the Bash or Sh. In this tutorial, we will learn how to run shell script files in different ways.
Create Shell Script or Script File
Before starting to run a shell script file we create a script file. Just create a text file with the *.sh extension. Then put some shell commands into it. In this example, we create the shell script named systeminfo.sh .
echo "This is a shell script."
Make Shell Script Executable and Run
The most command way to execute or run a shell script is making it executable and then call. The shell file is just a file with some text by default. It can be made executable by the shell by using the chmod command. In the following example we make the systeminfo.sh shell script file executeable for the current user.
chmod u+x systeminfo.sh
We can check if the shell script file is executable with the ls command like below.
ls -l systeminfo.sh
The output is like below where the user has the execute permission which is depicted with the x letter.
-rwxrw-r-- 1 ismail ismail 44 Oca 28 04:18 systeminfo.sh
Now we can run or execute the script file directly by calling with its name and path. If the script file is in the current working directory it can be called like below.
Alternatively if the shell script if is another directory then the current working directory we can specify the full or absolute path to execute it. This is the more reliable way where the script file can be executed properly for every situation.
Run Shell Script Directly with bash Command
Every bash shell command is interpreted and executed via the bash interpreter. The bash interpreter is an executable file which is located /usr/bin/bash . We can execute a shell script file directly providing it into the bash binary. This method do not requires making script file executeable. In the following example we execute the shell script file via bash binary or command.
Alternatively the full path of the shell script file can be specified like below.
Run Shell Script As Root
By default, the shell script is executed as the current user with the current user privileges. But in some cases, we may need to run the shell script file as root or with root privileges. We can use the sudo command during execution of the shell script file.
With the absolute path of the shell script file the sudo command can be merged like below.
Run Shell Script via GUI (Desktop Environment)
Shell script files can be also run via the GUI or Desktop environment. But first it should be enabled for the execution. Right click on the script file and click Properties like below.
In the Properties windows navigate to the Permissions tab and check the Execute configuration. This allow executing file as program.
Debug Shell Script While Running
The shell script file is like an application that executed different commands. The shell script can be debugged in order to detect bugs or errors. Also, the shell script file can be debugged to get detailed information about the execution of the shell script. The -x parameter is used with the bash command to debug the shell script file.
bash -x ./systeminfo.sh
Save Shell Script Output Into A File
Some shell scripts create some output that is printed into the standard output or terminal. This output can be redirected into a file easily by using the > operator. In the following example, we redirect the “systeminfo.sh” shell script output into the file named “output.txt”.
./systeminfo.sh > output.txt