Find the answer to your Linux question:
Results 1 to 6 of 6
hi, new to the forums, and kind of new to linux. recently i've been experimenting with shell scripts for work purposes i've been just kicking them off with sh <scriptname> ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2

    running shell scripts, which was is best

    hi, new to the forums, and kind of new to linux. recently i've been experimenting with shell scripts for work purposes
    i've been just kicking them off with sh <scriptname> but have been told there are other ways to start the script, but no one can really give any details, just that broad statement. Could someone clarify the different ways to say start example.sh and why?

    thanks!

    only realized now I have a typo in the subject, but it won't let me edit it
    Last edited by shok; 01-07-2008 at 06:33 PM. Reason: typo in the subject

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    1. Make the first line of your script #! /bin/bash.
    2. Make the file executable
    3. Execute it with ./example.sh.

    If you make a bin directory within your home directory and put your scripts there, you can execute them simply with example.sh, like any other normal command.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2
    Thank you for taking the time to reply:
    Quote Originally Posted by hazel View Post
    1. Make the first line of your script #! /bin/bash.
    yep did that
    2. Make the file executable
    Yes did chmod 755 I believe
    3. Execute it with ./example.sh.
    Yes I can execute it that way, along with sh example.sh
    If you make a bin directory within your home directory and put your scripts there, you can execute them simply with example.sh, like any other normal command.
    Cool I will try that, would that basically sum up the different ways to kick off a shell script?

  4. #4
    Linux User dxqcanada's Avatar
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    259
    That first line represents the shell interpretor that is going to be used.

    I would not say there is one that is "better". There are a number of shells bash, ksh, csh ... etc. Each have slight variances as they way they work ...
    The differences are too extensive to list here.

    /bin/sh is just a pointer to a shell
    You can find out by:
    Code:
    # ls -l /bin/sh
    I would recommend this book: O'Reilly Media | Classic Shell Scripting



    Men occasionally stumble over the truth,
    but most of them pick themselves up
    and hurry off as if nothing had happened.

    Winston Churchill


    ... then the Unix-Gods created "man" ...

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    There is no "best" way. There are just different ways. The two main ones of these (and as far as I know, the only ones) are the ones mentioned here, using the shebang line and by specifically invoking the interpreter.

    The shebang line is the '#!' mentioned above. This is literally hash-bang, which is where the name comes from. If you have a shebang line present, then when Linux tries to execute this file, it will hook the interpreter named in the shebang line and feed the script to it. This works because in Bash (and as far as I know, every interpreter that uses a shebang), '#' is the comment character, so the shebang line gets ignored by the interpreter.

    This is a very nice way of handling things because the interpreter is explicitly given in the script. I don't need to figure out if a script is written for Bash, sh, Perl, Ruby, csh, etc. It just executes properly.

    The other possibility is explicitly naming the interpreter. You can say "sh", "bash", "perl", "ruby", etc. The only advantage I can think of to this is that the file does not need to be executable (as instead of being treated as an executable, it is simply being read by the interpreter). However, I am not a particular fan of this method.

    It really depends on whichever you want. However, since the shebang line allows you to treat the script as you would any other executable (you no longer need to remember if it's a compiled executable or a script), it is quite popular.
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    There should actually not be a space after the shebang, it should look like this:
    #!/bin/bash
    Some interpreters have issues if there's a space there. And often /bin/sh is not just pointing to another shell, and is actually a bourne compliant shell, such as ash. If writing a bash script, be sure to use /bin/bash and not /bin/sh because using bash features could cause problems if /bin/sh is not bash.

Posting Permissions

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