Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
According to shell check the shebang is wrong and you have undeclared variable. I modified it to actually use bash as intended and put the variable in a correct place and it works fine. Oh and it said at one point to double quote the $hisvar as well to have zero issues with it, though that disappeared when I put my version in it.
Well, #!/usr/bin/env hisvar=8 /bin/bash is treated as
Code:
env hisvar='8 /bin/bash'
i.e. assigning a variable without running any command. When you run it on the command line it will just print the environment. But when you're calling it from a shebang it will try to run it like this
Well, #!/usr/bin/env hisvar=8 /bin/bash is treated as
Code:
env hisvar='8 /bin/bash'
i.e. assigning a variable without running any command. When you run it on the command line it will just print the environment. But when you're calling it from a shebang it will try to run it like this
shebang.sh, which was intended as an argument to bash, now becomes a command. This should happen for both
Code:
#!/usr/bin/bash
and
Code:
#!/usr/bin/env
but it happens only for the second. Why?
No. This shouldn't happen for both. Bash expects a script as an argument, passing a variable instead would end in an error, you wouldn't get an infinite loop.
No. This shouldn't happen for both. Bash expects a script as an argument, passing a variable instead would end in an error, you wouldn't get an infinite loop.
You are right again, bash expects a script. env works for binaries and scripts.
But this is a script. I was not explicit enough.
What are you missing? How is this situation different from the one described in #5?
Shebang instructs the system to pass the script as an argument to the command specified in the shebang.
So, when the system encounters a script with the shebang #!/usr/bin/bash, it will be passed as an argument to /usr/bin/bash. Like this
Code:
/usr/bin/bash ./bash.sh
As the script is empty (for Bash, shebang is just a comment as any other), it does nothing.
And when the system encounters a script with the shebang #!/usr/bin/env, it will be passed as an argument to /usr/bin/env. Like this
Code:
/usr/bin/env ./env.sh
As the script is executable and the path to it is specified, env just tries to execute the script as if ./env.sh were specified at the command prompt. But this is the script with a shebang, so the system calls the program specified in the shebang (env) and passes it the script as argument for execution. And the whole process repeats itself. Over and over again.
What are you missing? How is this situation different from the one described in #5?
Shebang instructs the system to pass the script as an argument to the command specified in the shebang.
Quote:
Originally Posted by MadeInGermany
bash sees the shebang as a comment in the script.
This is the piece of information I was missing.
The fact that bash ignores the shebang is even more shocking to me than the fact that bash does not run binaries. This may be done precisely to stop the recursion.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.