Home –  How to – How to run a Linux Shell Script?

How to run a Linux Shell Script?

Have you ever wondered how you can run a script in Linux?
Every script that needs to run should have a execute permission. By default it is refrained from such permission. One has to assign execute bit manually. In order to do so execute following command:

#chmod +x SomeScriptFile
#chmod 0755 SomeScriptFile

ls command can be used to check permissions on files:
# ls -l /bin/uname

-rwxr-xr-x. 1 root root 25948 Feb 8 2011 /bin/uname

[uname is being used for an example, it can be replaced with any script]

To run script:
$ ./SomeScriptFile [Enter]

You can also execute by specifying its full path
$ /full/path/here/SomeScriptFile.sh

For executing it in ksh shell:
$ ksh SomeScriptFile

For executing in bash shell :
$ bash SomeScriptFile


Creating a test shell script test.sh, any text editor can be used.
echo “ Hello, Free World”
echo “ You are at : $(pwd)”

After saving this file, set the execute bit:
$ chmod +x test.sh
$ ./test.sh

./ can be omitted if current directory has its entry in PATH variable. It is advised to have your own bin directory. Add this directory to PATH variable using export command. Move your recently created script in this directory and save all the hassle of writing whole path, now you can execute this command from anywhere. This procedure is shown below:

$ mkdir $HOME/bin
$ export PATH=$PATH:$HOME/bin
$ mv test.sh $HOME/bin
$ test.sh

Leave a Reply

Your email address will not be published. Required fields are marked *