In this post we will see  how to run a shell script you just received or created. There are many ways we can execute a shell script and some of the ways you don't require execute permissions as well on that script. There is a perception that to execute a shell script we require execute permissions. This is not true. Let us see how can we execute a shell script and know other methods to do the same. We will explain each and every method and what happens internally.

 

Below are some ways(Which I know and you can add more if you know other ways) we can execute a shell script in Linux.

Executing shell script in Linux (7 ways)

 

  1. source scriptname

  2. . scriptname

  3. interpreter scriptname(Ex: bash scriptname or sh scriptname or ksh script)

  4. ./scriptname (Execute permissions required)

  5. /path/to/scriptname (Execute permissions required)

  6. Linux command by setting the PATH (Execute permissions required)

  7. move to /bin or /sbin (Execute permissions required)

 

Executing a shell script with source command or . command

Source is a Linux/Unix built-in command which do not require execute permissions on a shell script to execute it. What actually a source command do is it just read the file and execute the commands one after the other by using present shell where user logged in.

For our understanding we use following simple shell script.

 

[surendra@linuxnix:~/scripts/sh]$ cat firstscript.sh
#!/bin/bash
echo "This is my first line"
echo $SHELL
echo "Bye in third line"
[surendra@linuxnix:~/scripts/sh]$ ls -l
total 8
-rw-rw-r– 1 visplad visplad  78 Apr 15 18:13 firstscript.sh
-rw-rw-r– 1 visplad visplad 111 Apr 11 18:09 test.sh

As said earlier running shell scripts do not require execute permissions for at least first three ways in the above list.

Running a shell scripting through source or . command

source scriptname

or

. scriptname

Example:

[surendra@linuxnix:~/scripts/sh]$ source firstscript.sh

This is my first line

/bin/bash

Bye in third line

[surendra@linuxnix:~/scripts/sh]$

With . command which is equalent to source command

[surendra@linuxnix:~/scripts/sh]$ . firstscript.sh

This is my first line

/bin/bash

Bye in third line

[surendra@linuxnix:~/scripts/sh]$

Make a note that executing . scriptname is different from ./scriptname which require execute permissions. We will see what ./scriptname below.

If you observe we do not get “Permission denied” errors.

Execute a script with interpreter(bash, ksh, csh, sh etc)

We can execute a shell script with different shell interpreater such as bash, ksh, dash, csh, tcsh, sh etc and again these do not require any execute permissions.

Example:

[surendra@linuxnix:~/scripts/sh]$ bash firstscript.sh

This is my first line

/bin/bash

Bye in third line

[surendra@linuxnix:~/scripts/sh]$ sh firstscript.sh

This is my first line

/bin/bash

Bye in third line

[surendra@linuxnix:~/scripts/sh]$ dash firstscript.sh

This is my first line

/bin/bash

Bye in third line

[surendra@linuxnix:~/scripts/sh]$

O.K what happens when you are executing with different interpreter? The Shebang/Hashbang statement is not consider when executing with different interpreter. Suppose you write a script in KSH and running it with bash interpreter then script is treated as bash script instead ksh script and you may face syntax issues as well when you try to execute a script written in one shell and executing with other shell.

Executing Linux shell script with ./ notation

Now let’s try to execute a script without setting executable permissions to the shell script file and see what happens?.

surendra@linuxnix:~/scripts/sh$ ./firstscript.sh

bash: ./firstscript.sh: Permission denied

surendra@linuxnix:~/scripts/sh$

So before executing a shell script with ./ notation it is advisable to change the permissions to executable. Here are the steps to execute your shell script through ./ notation.

Step1: Change the permissions of shell script to executable.

chmod +x firstscript.sh

 

Note1: The above command will give execute permissions to everyone. If you don’t want to give execute permissions to all and want to give execute permission to owner you can use below command

chmod u+x firstscript.sh

 

Note2: There is no need to have .sh as extension, just execute permissions are required to execute a script.

 

Note3: It’s not advisable to give 777 permissions to a script to execute it.

 

Step2:Now run the script

Go to script location and execute as blow

./firstscript.sh

Executing script by entering complete PATH for the script

Syntax:

/path/to/my/scriptname

Example:

surendra@linuxnix:~/scripts/sh$ /home/surendra/scripts/sh/firstscript.sh

This is my first line

/bin/bash

Bye in third line

surendra@linuxnix:~/scripts/sh$

Execute shell scripts like Linux commands

We can execute shell scripts like normal Linux/Unix commands by setting PATH variable to your scripts location. Suppose our scripts are located in /home/surendra/scripts/sh then if we add this path to our PATH variable then we no need to use ./ or complete path or any other sort to execute a script we just have to run our script with it’s name from any location

Setting PATH variable:

PATH=$PATH:/home/surendra/scripts/sh

surendra@linuxnix:~/scripts/sh$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

surendra@linuxnix:~/scripts/sh$ PATH=$PATH:/home/surendra/scripts/sh

surendra@linuxnix:~/scripts/sh$ cd

surendra@linuxnix:~$ firstscript.sh

This is my first line

/bin/bash

Bye in third line

surendra@linuxnix:~$ cd /var

surendra@linuxnix:/var$ firstscript.sh

This is my first line

/bin/bash

Bye in third line

Executing scripts by moving them to /bin or /sbin folder

This can be done only by root user. We can move all your scripts to one of these folders so that we can exeuct them without pointing to any interpreter.

Stay tuned to our next post on how to debug your shell scripts.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.