Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
scripts with sh extension are supposed to be "sourced" and don't need the x bit.
It may not work if you run it as ./script because this form will open a new shell and this subshell will only inherit of environment variables, not local variables of the calling shell.
Hope its more or less clear
"sh script" is a form I never use. ??
But it's supposed to be the same as ./script. The difference is with the "sh script" form, you donÄt need the x bit
suppose this is script.sh:
#!/bin/sh
echo "Hello"
echo $var
Now
var="world"
./script.sh
=>perm denied
sh ./script.sh
=>Hello
. ./script.sh
=>Hello world
chmod +x script.sh
./script.sh or sh ./script.sh
=>Hello -> We don't have var in the subshell (which is sh in the first case because of #!/bin/sh)
scripts with sh extension are supposed to be "sourced" and don't need the x bit.
Never read that rule? AFAIK ".sh" means "open with $PATH/sh if I don't know what to open it with".
"sh script" is a form I never use. ??
You use that to execute the script if it hasn't got the execute bit set.
AFAIK "./" means execute here, "at this location" which is something different than to source: ". something" (notice the space). Doing "sh ./Budget-Linux-bld1.sh" as non-root user runs the script with /bin/bash as shell and returns aprox 17 lines of help message. No problem here.
scripts with sh extension are supposed to be "sourced" and don't need the x bit.
Never read that rule? AFAIK ".sh" means "open with $PATH/sh if I don't know what to open it with".
If it has no shebang then its supposed to be sourced. As it has no shebang you have to know with what to run it, so that's why it has an extension (which is actually called suffix in linux).
Now running sh script or . script depends on you want to do (in general you refer to the README): For example, if you run several scripts after the other, doing . script or sh script is not the same and with my super-example, running it with sh script fails.
Quote:
"sh script" is a form I never use. ??
You use that to execute the script if it hasn't got the execute bit set.
Yes and you force sh to execute it. If your shell is bash and the shebang is /bin/sh, you will notice (on my system at least) that the shebang is not followed and your script will be executed by /bin/bash. That's probably a bash feature.
Quote:
AFAIK "./" means execute here, "at this location" which is something different than to source: ". something" (notice the space). Doing "sh ./Budget-Linux-bld1.sh" as non-root user runs the script with /bin/bash as shell and returns aprox 17 lines of help message. No problem here.
Here yes. But maybe there not. I'm sure you get the point
Just wanted to clear things, look at the documentation and do what it says...
If it has no shebang then its supposed to be sourced.
Could you please tell me where it says so? Not trying to catch you on words but "supposed" means it's not mandated. More like a convention you can or can decline to follow. It ain't in 'man bash' anyway.
As it has no shebang you have to know with what to run it, so that's why it has an extension (which is actually called suffix in linux).
AFAIK extensions are only for human recognition and applications that can't or won't find the associated application to open it with w/o "magic", MIME or equivalent. Binaries need no extension by default and neither do scripts. (And lets make it clear I mean 'man 5 magic'). Linking an application to a file by extension alone is a dumb move anyway as anyone who has messed with it knows.
If the script is executable and there's no shebang line the current shell will have execve() determine what to run it with, which in case of scripts means the default system shell. Extension doesn't matter. If the script is sourced permission and shebang line mean zilch since sourcing means "execute in the current shell" as does "sh script". Extension still doesn't matter.
Here yes. But maybe there not. I'm sure you get the point
No, I mean "here" as in "from that location", not as in "on my box".
If it has no shebang then its supposed to be sourced.
Could you please tell me where it says so? Not trying to catch you on words but "supposed" means it's not mandated. More like a convention you can or can decline to follow. It ain't in 'man bash' anyway. As it has no shebang you have to know with what to run it, so that's why it has an extension (which is actually called suffix in linux).
AFAIK extensions are only for human recognition and applications that can't or won't find the associated application to open it with w/o "magic", MIME or equivalent. Binaries need no extension by default and neither do scripts. (And lets make it clear I mean 'man 5 magic'). Linking an application to a file by extension alone is a dumb move anyway as anyone who has messed with it knows.
Yes its a convention I had read on some linux mailing list. Can't remember where sorry. Not in man bash that's sure.
Then give me an explanation for this suffix? I agree suffixes are stupid unless needed..
How do you know from a script with which shell its supposed to run, if there's no shebang?
Take as example I'm running tcsh as my /bin/sh link
Quote:
If the script is executable and there's no shebang line the current shell will have execve() determine what to run it with, which in case of scripts means the default system shell. Extension doesn't matter. If the script is sourced permission and shebang line mean zilch since sourcing means "execute in the current shell" as does "sh script". Extension still doesn't matter.
sh script opens a new shell I think (see my example)
Quote:
Here yes. But maybe there not. I'm sure you get the point
No, I mean "here" as in "from that location", not as in "on my box".
Quote:
Doing "sh ./Budget-Linux-bld1.sh" as non-root user runs the script with /bin/bash as shell and returns aprox 17 lines of help message. No problem here.
And what about my script? It doesn't work if you call it by sh ./script.
It will fail on every machine, in every location.
Sorry but English is not my native language... every word has an importance and sometimes I miss what people really mean. /me Frustrated.
I'm surprised that I haven't been understood...
My point was: If a script is supposed to be sourced and you execute it, it may not work.
I downloaded a few programs, go to execute them, but get message:
bash: ./Budget-Linux-bld1.sh: Permission denied
There are three ways of handling that. The usual way is to give the file execute permissions:
Code:
chmod +x Budget-Linux-bld1.sh
Or you can call bash explicitly, so that the file name is an ordinary argument to a command:
Code:
bash Budget-Linux-bld1.sh
Or you can source the file:
Code:
. Budget-Linux-bld1.sh
...which is the same as:
Code:
source Budget-Linux-bld1.sh
Note that when you source a script, it is executed in the current environment, and variables in your current shell may be changed. Also, if an exit command is encountered, it will exit your interactive shell (or the calling script, it it is sourced from another script).
Quote:
Originally Posted by RugbyPete
Im in as root, any ideas?
You shouldn't be logged in as root unless you absolutely have to be.
If it has no shebang then its supposed to be sourced.
That is not the case. The only reason for a shebang is to tell the system (or the shell) to use an interpreter other than the default (usually /bin/sh).
How do you know from a script with which shell its supposed to run, if there's no shebang?
I don't. I can run it. If it isn't compatible I can guess or look at it or make it compatible (or if there's a suffix maybe take the hint ;-p ).
And what about my script? It doesn't work if you call it by sh ./script. It will fail on every machine, in every location.
Uh. Do you run tcsh everywhere?
My point was: If a script is supposed to be sourced and you execute it, it may not work.
Good point, but practically speaking most scripts are meant to be executed, not sourced, right? At least in my experience. The only things I source are static resources like function libraries and config files.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.