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> ...
- 01-06-2008 #1Just 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 itLast edited by shok; 01-07-2008 at 06:33 PM. Reason: typo in the subject
- 01-07-2008 #2
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!"
- 01-07-2008 #3Just Joined!
- Join Date
- Jan 2008
- Posts
- 2
Thank you for taking the time to reply:
yep did that
Yes did chmod 755 I believe2. Make the file executable
Yes I can execute it that way, along with sh example.sh3. Execute it with ./example.sh.
Cool I will try that, would that basically sum up the different ways to kick off a shell script?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.
- 01-08-2008 #4
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:I would recommend this book: O'Reilly Media | Classic Shell ScriptingCode:# ls -l /bin/sh
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" ...
- 01-09-2008 #5
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
- 01-09-2008 #6Linux 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.


Reply With Quote
