HowTo Run a Script In Linux

by on November 10, 2011 · 13 comments· LAST UPDATED November 4, 2012


How do I run a Linux shell script? How can I run a script in Linux operating system using command line options?

By default shell script will not run. You need to set execute permission for your shell script. To execute or run script type the following command:

Tutorial details
DifficultyEasy (rss)
Root privilegesNo
Estimated completion timeN/A

chmod +x script-name-here

chmod 0755

Next, use the ls command to view permission on the script:
$ ls -l script-name-here
To execute the script, type:
$ ./script-name-here

You can also run a script using any one of the following syntax:
$ /path/to/shell/script/

Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh

Run a script called backup.bash using BASH shell:
$ bash backup.bash


Create a shell script called using a text editor such as vi or gedit:

echo "Hello $USER."
echo "Today is $(date)"
echo "Current working directory : $(pwd)"

Save and close the file. Set the permission:
$ chmod +x
Run the script:
$ ./

If the current directory is in the PATH variable, you can avoid typing the ./ before the It is a good idea to create your own bin directory as follows:
$ mkdir $HOME/bin
Add $HOME/bin to the PATH variable using bash shell export command:
$ export PATH=$PATH:$HOME/bin
$ echo $PATH

Move in $HOME/bin using the mv command, run:
$ mv $HOME/bin
Execute the script:
Sample outputs:

Hello vivek.
Today is Thu Nov 10 17:49:15 IST 2011
Current working directory : /nafiler05/users/v/vivek/bin/demos
See also:
Tweet itFacebook itG+ itDownload PDF versionFound an error/typo on this page?

{ 13 comments… read them below or add one }

1 georges November 12, 2011 at 1:50 pm

this is very good. It’s just missing one thing. the export command should be added in in the ~/.bashrc file to survive across sessions.
$ tail -1 ~/.bashrc

or the equivalent for your chosen shell (bs, ksh, tcsh etc …)


2 Indy November 12, 2011 at 7:01 pm

For compiling C prgms,
cc hello.c

btw, Why don’t you have categories in WP?


3 nixCraft November 13, 2011 at 10:47 am

Click on category icon (hint: Bash shell).


4 Halil November 23, 2011 at 8:59 pm

Hello Vivek, I was not aware that that image is a category link, too :) It really looks like a post image. I’m referencing this site from time to time, and I’ve learned that you have categories just now! I’d really suggest using a different method for showing categories, unless you want to hide them :) Thanks for the great site.


5 surendra December 20, 2011 at 3:56 pm

we can run a script even with out permissions using source command



6 barun kumar February 1, 2012 at 7:03 pm

no dear if u want to run script the u hv to give execute permission on that file

chmod 755 or 777


7 Lewallen October 6, 2014 at 9:23 pm

Don’t be a patronizing shit…


8 barun kumar February 1, 2012 at 7:01 pm

hi,,,, i m facing problem to write for setup ip address ,subnet and gateway in linux

kindly suggest me how i do…
if u have the give me idea…


9 Bucky April 29, 2012 at 6:10 am

Good information. I am trying to run a sql using a shell command.


10 Aiswarya October 4, 2013 at 4:22 pm

Please provide the info..

How to run the Unix command only during the even and odd hours only.

Kindly provide the command to incorporate in the script.


11 Alejandro March 2, 2014 at 7:56 pm

I am having a problem related to this. I have downloaded an program and it has a GUI written in Java. I need to run the script to launch this program, with “sudo” privileges in order to having it running properly. I don’t want to navigate to the folder where this script is, every time I need it, therefore I first made it executable and added the folder to the PATH. Now, when I write “sudo script-name”, I get “script-name not command found”, if I write only “script-name”, it finds it but it doesn’t run properly. Is there a way to launch a script, that is in the PATH, with sudo privileges? Thank you in advance.


12 Nix Craft March 3, 2014 at 7:07 am

Run it as follows:

## if path to script is /home/foo/
sudo /home/foo/

Or cd to /home/foo and run:

cd /home/foo/
sudo ./


13 Alejandro March 3, 2014 at 10:08 am

Thank you. I decide to add it as an alias: “alias script-name=’sudo bash /path/to/script/script-name”


Leave a Comment

Tagged as: , , , , , , , , , , , , ,

Previous Faq:

Next Faq: