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:
chmod +x script-name-here
OR
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/backup.sh
Run a script called backup.ksh using ksh shell:
$ ksh backup.ksh
Run a script called backup.bash using BASH shell:
$ bash backup.bash
Example
Create a shell script called hello.sh using a text editor such as vi or gedit:
#!/bin/bash echo "Hello $USER." echo "Today is $(date)" echo "Current working directory : $(pwd)"
Save and close the file. Set the permission:
$ chmod +x hello.sh
Run the script:
$ ./hello.sh
If the current directory is in the PATH variable, you can avoid typing the ./ before the hello.sh. 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 hello.sh in $HOME/bin using the mv command, run:
$ mv hello.sh $HOME/bin
Execute the script:
$ hello.sh
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:
- Hello, World! Tutorial from the Linux shell scripting wiki.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop












{ 8 comments… read them below or add one }
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
PATH=$PATH:~/bin
or the equivalent for your chosen shell (bs, ksh, tcsh etc …)
Nice.
For compiling C prgms,
cc hello.c
./a.out
btw, Why don’t you have categories in WP?
Click on category icon (hint: Bash shell).
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.
we can run a script even with out permissions using source command
sourch scriptname.sh
Thanks,
Surendra.
no dear if u want to run script the u hv to give execute permission on that file
like….scriptname.sh
chmod 755 or 777 scriptname.sh
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…
Good information. I am trying to run a sql using a shell command.