How to execute Shell script in Linux

 Knowing how to run scripts is incredibly useful when automating tasks, setting up cron jobs, or just trying to manage system activity. However, there is a certain joy in writing scripts that not only work, but are also elegant and neat. Let's learn how to execute shell scripts in Linux through the following article!

What is a shell script?

Before we dive into execution, let's quickly review what a shell script is. A shell script is a text file that contains a series of commands for a Unix-based shell, such as Bash, to execute. It's essentially a program written in the shell's command-line language.

How to execute Shell script in Linux

Step 1: Create a simple shell script

The first step to executing a script is, of course, writing it. For this example, let's create a simple script that reads "Hello, World!" and the current date.

Open a terminal on your Ubuntu system and use your favorite text editor to create a new file. For example, nano is for quick edits because it's simple, but you might prefer vim if you need something more powerful.

nano hello_world.sh

Inside the editor, enter the following script:

#!/bin/bash echo "Hello, World!" echo "Today's date is: 30/09/2024"

This script does two things: It prints "Hello, World!" and then displays the current date. The #!/bin/bash at the top is called the shebang and it tells the system that this script should be run in the Bash shell.

Step 2: Make the script executable

By default, the script you just created is not executable. You need to make it executable using the chmod command. This is one of those steps that is easy to forget but very important.

chmod +x hello_world.sh

The +x option makes the script executable by the user, group, and others, depending on your permissions settings.

Step 3: Execute the shell script

Now, you are ready to run the script. There are several ways to execute a script in Linux and this article will cover the most common methods:

Method 1: Execute using relative path

This is the simplest method if you are in the same directory as the script:

./hello_world.sh

./ tells the shell to look for the script in the current directory. If all goes well, you should see the following output:

Hello, World! Today's date is: Tue Aug 8 14:22:31 UTC 2024

Method 2: Execute with absolute path

Sometimes you will need to specify the full path to the script, especially if the script is located in another directory.

/home/username/scripts/hello_world.sh

This method is very convenient when running scripts from cron jobs or in other scripts.

Method 3: Execute using a specific shell

Although the shebang specifies which shell to use, you can override this by running the script in a different shell. For example, if you want to run the script in sh instead of bash, you can execute:

sh hello_world.sh

Or, explicitly in bash:

bash hello_world.sh

This can be useful when testing how your script works in different shells.

Troubleshooting common problems

Even simple scripts can have problems. Here are some common problems and how to fix them:

  1. Permission denied : If you see this error, double check that you have executed the script using chmod +x command .
  2. Command not found : This usually means that the script's shebang line is missing or incorrect, or the script is not in your PATH.
  3. Script not running as expected : Use bash -x followed by the script name to run the script in debug mode. This will display each command as it is executed, which is helpful for figuring out where the error is.

Executing shell scripts in Linux is a basic task that opens up a world of automation and efficiency. For tasks like server management or just trying to automate a few things on your local machine, shell scripting is a skill worth mastering. It's satisfying to see a well-written script run flawlessly, saving time and reducing the chance of human error.

4.5 ★ | 2 Vote

May be interested

  • What is Linux Shell? The most popular Linux ShellsWhat is Linux Shell? The most popular Linux Shells
    are you satisfied using the bash shell in linux? or do you want to try an alternative? there are tsch, fish, kornshell and z shell for you to choose from. but which popular linux shell is best?
  • Flow control in ShellFlow control in Shell
    while writing a shell script, there may be a situation when you need to follow an external path 2 provided. so you need to create usage of conditional commands that allow your program to make accurate decisions and perform the right actions.
  • Basic Shell commands in LinuxBasic Shell commands in Linux
    working on the linux operating system you cannot not know the basic shell commands. for ease of operation on linux, tipsmake.com has compiled some basic linux shell commands, please consult.
  • How to execute Linux commands in the backgroundHow to execute Linux commands in the background
    linux commands are a great way to interact with the system using terminal. however, it can sometimes take a while to complete the task at hand. this forces the user to wait a significant amount of time or create a new shell entirely.
  • Basic Shell operatorsBasic Shell operators
    there are many operators supported by each shell. our tutorial is based on the default shell (bourne) so we are discussing bourne shell operators in this chapter.
  • Math functions are available in ShellMath functions are available in Shell
    most parts of the tutorial are mostly built on bourne shell, but this page lists all the built math functions available in the korn shell.
  • How to display the date and time in Linux TerminalHow to display the date and time in Linux Terminal
    let's take a look at the advanced uses of the date command in the command line and how to use it in the shell script to perform more functions than simply displaying the time.
  • The Linux machine can be remotely hacked with a poisoned DNS responseThe Linux machine can be remotely hacked with a poisoned DNS response
    an important vulnerability was discovered on systemd, the init system and managed on a linux machine, allowing the hacker to cause a buffer overflow to execute malicious code on the target machine via dns feedback.
  • Bring the best of Linux to Windows with CASHBring the best of Linux to Windows with CASH
    windows command prompt is like an annoying mess, rather than an indispensable part of the operating system. in contrast, linux is designed to be very elegant, while helping users easily work efficiently.
  • How to use Zsh (or other Shells) on Windows 10?How to use Zsh (or other Shells) on Windows 10?
    bash shell is integrated into windows 10 anniversary update version is just a compatible class to run linux software on windows operating system. you can use the bash shell to run zsh or any shell you want.