This is a small how-to topic on running the shell script you just received or created. The basic philosophy of Linux is to consider everything as files. And if you want to execute a file/app the permissions are set to executable on that file otherwise you will get permissions denied.

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

./numper.sh bash: ./numper.sh: Permission denied

So before executing a shell script it’s advisable to change the permissions to executable. Here are the steps to execute your shell script.

Step1: Change the permissions of shell script to executable.

chmod +x shellscriptfile

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 shellscriptfile

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

Method1:Go to script location and execute as blow

./scriptname

Method2: use sh command to execute it.

sh scriptname or bash scriptname or ksh scriptname

Method3: Use source command to execute a script.

source scriptname

Note: source command does not require shell script to have executable permissions for executing it.

Method4: Giving entier PATH for the script when executing

/path/to/my/script

Method5:Setting PATH variable to your scripts location.

 

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.