How To Run a Script In Linux

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

By default, the 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
Difficulty level Easy
Root privileges No
Requirements None
Est. reading time N/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

To run a script called backup.bash using BASH shell:
$ bash backup.bash

Examples that shows how to run a script in Linux

Create a shell script called using a text editor such as vi or gedit/nano:
Append the following code:

# My first shell script
# Author: nixCraft
# ----------------------------
echo "Hello $USER."
echo "Today is $(date)"
echo "Current working directory : $(pwd)"

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

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

You learned how to write a simple shell script and run a script in Linux operating system with help of chmod and other commands. Please see the following tutorials for more information on bash shell scripting under Linux or Unix-like operating systems:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 19 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
19 comments… add one
  • georges Nov 12, 2011 @ 13:50

    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 …)

    • haxorfire Aug 24, 2015 @ 15:16

      Just the solution I needed to figure this out, thanks man! :D

  • Indy Nov 12, 2011 @ 19:01

    For compiling C prgms,
    cc hello.c

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

    • 🐧 nixCraft Nov 13, 2011 @ 10:47

      Click on category icon (hint: Bash shell).

      • Halil Nov 23, 2011 @ 20:59

        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.

  • surendra Dec 20, 2011 @ 15:56

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


    • barun kumar Feb 1, 2012 @ 19:03

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

      chmod 755 or 777

      • Lewallen Oct 6, 2014 @ 21:23

        Don’t be a patronizing shit…

        • Henry Feb 18, 2016 @ 14:03

          two years late?

          • Taylor Jul 7, 2016 @ 17:27


  • barun kumar Feb 1, 2012 @ 19:01

    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…

    • vaibhav pawale Jun 29, 2015 @ 9:43

      int main()
      system(“c:\\windows\\your system name\\ip config”);
      return 0;

  • Bucky Apr 29, 2012 @ 6:10

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

  • Aiswarya Oct 4, 2013 @ 16:22

    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.

  • Alejandro Mar 2, 2014 @ 19:56

    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.

    • 🐧 Nix Craft Mar 3, 2014 @ 7:07

      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 ./
      • Alejandro Mar 3, 2014 @ 10:08

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

  • Jouni "rautamiekka" Järvinen Mar 26, 2016 @ 1:26

    What’s the difference with §. /PATH/TO/TARGET§ and §./PATH/TO/TARGET§ ?

  • Jafet Gmez Nov 3, 2016 @ 19:51

    thanx, im learning and your are a good teacher.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum