There are two ways to run a shell script in Linux. You can use:
bash script.sh
Or you can execute the shell script like this:
./script.sh
That maybe simple, but it doesn’t explain a lot. Don’t worry, I’ll do the necessary explaining with examples so that you understand why a particular syntax is used in the given format while running a shell script.
I am going to use this one line shell script to make things as uncomplicated as possible:
abhishek@itsfoss:~/Scripts$ cat hello.sh
echo "Hello World!"
Method 1: Running a shell script by passing the file as argument to shell
The first method involves passing the script file name as an argument to the shell.
Considering that 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 doesn’t need to have the execute permission. Pretty handy for quick and simple tasks.
If you are not familiar already, I advise you to read my detailed guide on file permission in Linux.
Keep in mind that it needs to be a shell script that you pass as argument. A shell script is composed of commands. If you use a normal text file, it will complain about incorrect commands.
In this approach, you explicitly specified that you want to use bash as the interpreter for the script.
Shell is just a program and bash is an implementation of that. There are other such shells program like ksh, zsh, etc. If you have other shells installed, you can use that as well instead of bash.
For example, I installed zsh and used it to run the same script:
Method 2: Execute shell script by specifying its path
The other method to run a shell script is by providing its path. But for that to be possible, your file must be executable. Otherwise, you’ll have “permission denied” error when you try to execute the script.
So first you need to make sure that your script has the 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 to type the file name along with 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 specify it the absolute or relative path to the script:
That ./ before the script is important (when you are in the same directory as the script)
Why can you not use the script name when you are in the same directory? That is because your Linux systems looks for the executables to run in a few selected directories that are specified in the PATH variable.
Here’s the value of PATH variable for my system:
abhishek@itsfoss:~$ 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 one 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
- /bin
- /usr/games
- /usr/local/games
- /snap/bin
The binaries or executable files for Linux commands like ls, cat etc are located in one of those directories. This is why you are able to run these commands from anywhere on your system just by using their names. See, the ls command is located in /usr/bin directory.
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 beginning of the shell scripts?
Remember how I mentioned that shell is just a program and there are different implementations of shells.
When you use the #! /bin/bash, you are specifying that the script is to run with bash as interpreter. If you don’t do that and run a script in ./script.sh manner, it is usually run with whatever shell you are running.
Does it matter? It could. See, most of the shell syntax is common in all kind of shell but some might differ.
For example, the array behavior is different in bash and zsh shells. In zsh, the array index starts at 1 instead of 0.
Using #! /bin/bash indicates that the script is bash shell script and should be run with bash as interpreter irrespective of the shell which is being used on the system. If you are using zsh specific syntax, you can indicate that it is zsh script by adding #! /bin/zsh as the first line of the script.
The space between #! /bin/bash doesn’t matter. You can also use #!/bin/bash.
Was it helpful?
I hope this article added to your Linux knowledge. If you still have questions or suggestions, please leave a comment.
Expert users can still nitpick this article about things I missed out. But the problem with such beginner topics is that it is not easy to find the right balance of information and avoid having too much or too few details.
If you are interested in learning bash script, we have an entire Bash Beginner Series on our sysadmin focused website Linux Handbook.
If you want, you may also purchase the ebook with additional exercises to support Linux Handbook.