Find the answer to your Linux question:
Results 1 to 5 of 5
Freinds, WHy is that soemtimes a shell script runs with sh command,sometimes with a "./" and sometimes with just the name. For ex, to a run a shell script "shscript", ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Jul 2007
    Posts
    144

    different ways to run a shell script


    Freinds,

    WHy is that soemtimes a shell script runs with sh command,sometimes with a "./" and sometimes with just the name.
    For ex,

    to a run a shell script "shscript", i issue
    $ sh shscript.sh
    $ ./shscript.sh
    $shscript

    Sometimes only the first option works not and 2 and 3 one.

    Thanks

  2. #2
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,132
    If a script has the execute permission, it can be executed simply by calling it. That is call it with
    Code:
    ./scriptname
    scriptname
    The distinction to be made is that scripts that are within directories in your $PATH such as /usr/bin will be automatically accessible. If the script is not in your $PATH you can call it using the full location of the script, e.g. /home/linuxuser_1/scriptname. If you want to execute a script in the current directory you use ./scriptname. In fact if you want to execute a script in the parent directory of your current location you can use ../scriptname , as . means current and .. means parent.

    As for the .sh extension, this is purely optional. File extensions are not required in Linux/Unix and are simply for user convenience.

  3. #3
    Linux Newbie
    Join Date
    Jul 2007
    Posts
    144
    Hi,
    Thanks for replying.

    So, all scipts with +x permission can be called as
    $ script.sh
    $script

    and those with no +x permission should be called as
    $ sh script.sh
    $./script
    ?

    I have a script that has executable permission but when i do
    $script.sh
    bash: script.sh: command not found

  4. #4
    Blackfooted Penguin daark.child's Avatar
    Join Date
    Apr 2006
    Location
    West Yorks
    Posts
    4,393
    If you get a command not found error it means that the script is probably not in your PATH.

  5. #5
    Just Joined! sanjaykatiyar's Avatar
    Join Date
    Mar 2007
    Location
    Bangalore
    Posts
    97
    I have a script that has executable permission but when i do
    $script.sh
    bash: script.sh: command not found
    Here you are running your script like a command or program which are in your $PATH.
    Eitther copy script.sh in $PATH values or simply use:
    $ ./script.sh

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •