ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The character '#!' at the beginning of a file lets the system know what program executes it,
e.g. #!/bin/bash or #!/bin/perl.
If you run 'sh foo.sh' you probably don't need this, however if you just type './foo.sh' you do.
I generally just put the #!/bin/<whatever> at the top and run scripts by running 'chmod u+x <script> and then './<script>'
The character '#!' at the beginning of a file lets the system know what program executes it,
e.g. #!/bin/bash or #!/bin/perl.
If you run 'sh foo.sh' you probably don't need this, however if you just type './foo.sh' you do.
No, the shebang is not necessary unless you are using an interpreter other than the default shell, or in special circumstances such as a CGI script.
Quote:
I generally just put the #!/bin/<whatever> at the top
I generally don't use a shebang unless it is for special use. I often move scripts between different systems. If the script requires bash, #!/bin/bash will not work on most of them (on one it's /usr/local/bin/bash, on another it's /usr/pkg/bin/bash, etc.).
Quote:
and run scripts by running 'chmod u+x <script> and then './<script>'
I never put scripts in the current directory. I always use a central directory ($HOME/bin or /usr/local/bin) which is in my PATH.
The character '#!' at the beginning of a file lets the system know what program executes it,
e.g. #!/bin/bash or #!/bin/perl.
If you run 'sh foo.sh' you probably don't need this, however if you just type './foo.sh' you do.
I generally just put the #!/bin/<whatever> at the top and run scripts by running 'chmod u+x <script> and then './<script>'
Your reply is great! I am wondering the only differences between executing shell script directly (method 1) and using sh to execute shell script (method 2) is, method 2 will use another shell to start the shell script, no other differences?
[INDENT]No, the shebang is not necessary unless you are using an interpreter other than the default shell, or in special circumstances such as a CGI script.
Two more questions,
1. What is *shebang*?
2. Why *the shebang is not necessary unless you are using an interpreter*? Show an example please?
The character sequence '#!' is colloquially called 'shebang',
e.g.
#!/bin/bash
or
#!/usr/bin/perl
If you are writing a shell script, it's usually advised (in prod systems at least) to specify (via the #! invocation) which shell is required to run this script properly.
Otherwise, what can happen is that eg you write it in bash, but the default shell on the target system turns out to be csh.
Not all bash cmds are understood by the csh shell.
Using the shebang line as described above means that you simplify the cmd line by not having to specify the actual shell by name externally to the script.
Note that eg cron has a minimal/almost non-existent default shell env, which means you should always specify the complete path for a cmd eg /usr/bin/perl and not the short form eg perl.
HTH
The character sequence '#!' is colloquially called 'shebang',
e.g.
#!/bin/bash
or
#!/usr/bin/perl
If you are writing a shell script, it's usually advised (in prod systems at least) to specify (via the #! invocation) which shell is required to run this script properly.
Otherwise, what can happen is that eg you write it in bash, but the default shell on the target system turns out to be csh.
It is just as likely that bash may be in a different location on the target system; a shebang makes the script less portable.
If you are writing a script to be used on more than one system, it is safest to use POSIX compliant code that can be run by /bin/sh on most systems.
Quote:
Not all bash cmds are understood by the csh shell.
"Not all" is an understatement. A Bourne-type shell (such as bash) is a completely different language from csh; it's not just the commands, but the syntax is very different.
Quote:
Using the shebang line as described above means that you simplify the cmd line by not having to specify the actual shell by name externally to the script.
You do not have to specify the shell on the command line if there is no shebang. If the script is written for /bin/sh, that will be used to execute the script even if you call it from csh.
Quote:
Note that eg cron has a minimal/almost non-existent default shell env, which means you should always specify the complete path for a cmd eg /usr/bin/perl and not the short form eg perl.
I want to confirm with you that there is no functonal differences between using sh to execute and execute the shell script directly, right?
In my shell script, I am using #!/bin/bash at the first line.
regards,
George
Quote:
Originally Posted by chrism01
The character sequence '#!' is colloquially called 'shebang',
e.g.
#!/bin/bash
or
#!/usr/bin/perl
If you are writing a shell script, it's usually advised (in prod systems at least) to specify (via the #! invocation) which shell is required to run this script properly.
Otherwise, what can happen is that eg you write it in bash, but the default shell on the target system turns out to be csh.
Not all bash cmds are understood by the csh shell.
Using the shebang line as described above means that you simplify the cmd line by not having to specify the actual shell by name externally to the script.
Note that eg cron has a minimal/almost non-existent default shell env, which means you should always specify the complete path for a cmd eg /usr/bin/perl and not the short form eg perl.
HTH
If I am using #!/bin/bash at the first line, there are any functional differences between executing the shell script directly and using sh to execute?
regards,
George
Quote:
Originally Posted by cfaj
It is just as likely that bash may be in a different location on the target system; a shebang makes the script less portable.
If you are writing a script to be used on more than one system, it is safest to use POSIX compliant code that can be run by /bin/sh on most systems.
"Not all" is an understatement. A Bourne-type shell (such as bash) is a completely different language from csh; it's not just the commands, but the syntax is very different.
You do not have to specify the shell on the command line if there is no shebang. If the script is written for /bin/sh, that will be used to execute the script even if you call it from csh.
AFAIK, nope. But what cfaj says is new to me. I like the idea of putting stuff in /usr/local/bin but most of my stuff has a specific purpose and not really general use, so I keep them in the directory related to the project.
If I am using #!/bin/bash at the first line, there are any functional differences between executing the shell script directly and using sh to execute?
If you are calling a script as the argument to a shell, the shebang makes no difference; it is just another comment. The shebang is only used when the script is executed as a command.
The portability of scripts is decreased if you specify an absolute path.
I once used Solaris often and ran into such problems going back and forth to and from Linux.
I use a perl utility, fixin (fix interpreter), that adjusts the shebang line of scripts for the correct path. It works not only for perl shebangs, but for anything you have in a shebang -- awk, sh, etc.
The point of specifying the shell your code requires (on production systems) is so that you don't end up with a script that partially runs, then screws up/crashes because it's running under the wrong intepreter.
It's much safer for it not to run at all if it can't find the correct interpreter.
It can cost a company a lot of money if it goes wrong...
It also tells the sysadmin/maintenance prog which shell it will work with, rather than them having to guess.
You might not be avail to be asked when it goes pear-shaped ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.