How to run a shell script in Linux [Essentials Explained]

0


[ad_1]

There are two ways to run a shell script on Linux. You can use:

bash script.sh

Or you can run the shell script like this:

./script.sh

It may be simple, but it doesn’t explain much. Don’t worry, I will explain it as necessary with examples so that you understand why a particular syntax is used in the given format when executing a shell script.

I’ll use this one-line shell script to make things as easy as possible:

[email protected]:~/Scripts$ cat hello.sh 

echo "Hello World!"

Method 1: Execute a shell script passing the file as argument to the shell

The first method is to pass the name of the script file as an argument to the shell.

Since bash is the default shell, you can run a script like this:

bash hello.sh

Do you know the advantage of this approach? Your script does not need to have execute permission. Quite handy for quick and easy tasks.

Run a Linux Shell Script

If you are not already familiar, I suggest you read my in-depth guide to file permissions in Linux.

Keep in mind that this must be a shell script that you pass as an argument. A shell script is made up of commands. If you are using a normal text file, it will complain about incorrect commands.

Running a text file as a script on Linux
Run a text file as a script

In this approach, you have explicitly specified that you want to use bash as an interpreter for the scenario.

Shell is just a program and bash is an implementation. There are other shell programs like ksh, zsh, etc. If you have other shells installed, you can also use them instead of bash.

For example, I installed zsh and used it to run the same script:

Running a shell script with Zsh
Running a shell script with Zsh

Recommended reading:

Method 2: Run the shell script by specifying its path

The other way to run a shell script is to provide its path. But for this to be possible, your file must be executable. Otherwise, you will get the “permission denied” error when you try to run the script.

So first you need to make sure that your script has execute permission. You can use the chmod command to give yourself this permission like this:

chmod u+x script.sh

Once your script is executable, all you need to do is enter the name of the file as well as its absolute or relative path. Most often you are in the same directory so you just use it like this:

./script.sh

If you are not in the same directory as your script, you can give it the absolute or relative path to the script:

Running the Shell script in another directory
Running the Shell script in another directory

This ./ before the script is important (when we are in the same directory as the script)

Running Linux shell scripts

Why can’t you use the script name when you are in the same directory? This is because your Linux systems are looking for executables to run in a few selected directories that are specified in the PATH variable.

Here is the value of the PATH variable for my system:

[email protected]:~$ echo $PATH
/home/abhishek/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

This means that any file with execute permissions in any of the following directories can be executed from anywhere in the system:

  • /home/abhishek/.local/bin
  • / usr / local / sbin
  • / usr / local / bin
  • / usr / sbin
  • / usr / bin
  • / sbin
  • /trash can
  • / usr / games
  • / usr / local / games
  • / snap / bin

Binaries or executable files for Linux commands like ls, cat etc are located in one of these directories. That’s why you can run these commands from anywhere on your system just using their names. See, the ls command is in the / usr / bin directory.

Locating the Linux command

When you specify the script WITHOUT the absolute or relative path, it cannot find it in the directories mentioned in the PATH variable.

Why most shell scripts contain #! / bin / bash at the start of shell scripts?

Remember how I mentioned that shell is just a program and that there are different implementations of shells.

When you use the #! / bin / bash, you specify that the script should run with bash as the interpreter. If you don’t and run a script like ./script.sh, it is usually executed with the shell you are running.

Does it matter? It could. You see, most of the shell syntax is common to all types of shell, but some may differ.

For example, the behavior of the array is different in bash and zsh shells. In zsh, the array index starts at 1 instead of 0.

Bash vs. Zsh
Bash vs. Zsh

Using #! / bin / bash indicates that the script is a bash shell script and should be run with bash as the interpreter regardless of the shell used on the system. If you are using zsh-specific syntax, you can indicate that this is a zsh script by appending #! / bin / zsh as the first line of the script.

The space between #! / bin / bash doesn’t matter. You can also use #! / Bin / bash.

Did you find this helpful ?

Hope this article has added to your Linux knowledge. If you still have questions or suggestions, please leave a comment.

Expert users can always nitpick this article on things I missed. But the problem with such beginner topics is that it’s not easy to find the right balance of information and avoid having too much or too little detail.

If you’re interested in learning the bash script, we have a whole Bash Beginner series on our Linux Handbook focused on the system administrator. If you want, you can also purchase the eBook with additional exercises to support the Linux manual.

[ad_2]

Leave A Reply

Your email address will not be published.