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)
source scriptname
. scriptname
interpreter scriptname(Ex: bash scriptname or sh scriptname or ksh script)
./scriptname (Execute permissions required)
/path/to/scriptname (Execute permissions required)
Linux command by setting the PATH (Execute permissions required)
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.
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.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018