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.
--------------------
./test.sh: line 1: Goo: command not found
./test.sh: line 1: Foo: command not found
./test.sh: line 1: Zoo: command not found
--------------------
I simply want to set three variables and test their output. Are there anything wrong with this script file?
I'm curious why you say to use exit at the end, instead of just naturally allowing EOF to terminate the script. Can Bash be configured to ignore EOF as a session terminator?
Also, I just want to point out, with respect, that I believe it's more flexible to use "#!/usr/bin/env bash" than "#!/bin/bash", since bash may not be installed into /bin (as it is not on BSD systems, for example). I think env is either stored in or symlinked in /usr/bin on every Unix-like installation.
Don't put parentheses around the variable substitutions; just do $Foo. But this line should be different:
Code:
Zoo="$Foo $Goo"
You have to quote this line if you want it to work, I think. In fact, you should quote any assignment that isn't a number, even if you don't have to.
Your method works! I used Makefile style to write bash files, so I add () around variables. I have also found another approach to work out this solution.
I'm curious why you say to use exit at the end, instead of just naturally allowing EOF to terminate the script. Can Bash be configured to ignore EOF as a session terminator?
Also, I just want to point out, with respect, that I believe it's more flexible to use "#!/usr/bin/env bash" than "#!/bin/bash", since bash may not be installed into /bin (as it is not on BSD systems, for example). I think env is either stored in or symlinked in /usr/bin on every Unix-like installation.
What is the function of env? I have never used it before. Is it available on both Linux and UNIX (like Solaris)?
@exit: it's something i picked up from over here: http://www.linuxcommand.org/wss0090.php#exit
I thought including exit on the last line of code caused a script to terminate succesfully (so no errors were made).
The script exits with the result value of the last cmd run. If you want to set it explictly, use
exit <some_positive_integer_or_zero>
conventionally on Unix style systems, zero is success, (positive) int is an error. iirc, negative value is not allowed here.
From the manual page: "The env utility executes another utility after modifying the environment as specified on the command line. ... Probably the most common use of env is to find the correct interpreter for a script, when the interpreter may be in different directories on different systems. The following example will find the `perl' interpreter by searching through the directories specified by PATH."
Code:
#!/usr/bin/env perl
In other words, env applies an environment (the current one, modified by options passed to env) to an executable. So actually, you can use env to specify all kinds of environment variables, including PATH and LD_LIBRARY_PATH, so that things are just right when the script gets executed. This feature would be increasingly useful in varied environments. The env binary is stored in /usr/bin on all three of the major BSDs, and also on Slackware Linux, in my experience. I think it's somewhat of a standard, so it should be there on other systems as well, but you'll have to check to be sure.
Cool muha! I have never used it in this way before. I think there must be some default return values if I do not call exit explicitly at the last line of a script. Right?
Quote:
Originally Posted by muha
@exit: it's something i picked up from over here: http://www.linuxcommand.org/wss0090.php#exit
I thought including exit on the last line of code caused a script to terminate succesfully (so no errors were made).
The script exits with the result value of the last cmd run. If you want to set it explictly, use
exit <some_positive_integer_or_zero>
conventionally on Unix style systems, zero is success, (positive) int is an error. iirc, negative value is not allowed here.
Do you know whether there is some default value to return if we do not write exit on the last line of a script file?
From the manual page: "The env utility executes another utility after modifying the environment as specified on the command line. ... Probably the most common use of env is to find the correct interpreter for a script, when the interpreter may be in different directories on different systems. The following example will find the `perl' interpreter by searching through the directories specified by PATH."
Code:
#!/usr/bin/env perl
In other words, env applies an environment (the current one, modified by options passed to env) to an executable. So actually, you can use env to specify all kinds of environment variables, including PATH and LD_LIBRARY_PATH, so that things are just right when the script gets executed. This feature would be increasingly useful in varied environments. The env binary is stored in /usr/bin on all three of the major BSDs, and also on Slackware Linux, in my experience. I think it's somewhat of a standard, so it should be there on other systems as well, but you'll have to check to be sure.
What do you mean "the current one, modified by options passed to env"? I think you mean passing parameter to env -- but I am not sure about it. Could you show me an example to show your idea please?
I mean, the current environment is applied to the program that env is going to execute. And by passing VARIABLE=value options to env, you can modify that environment. For example:
MY_ENV_VARIABLE probably won't exist before the script starts executing. But when you call env, it creates the variable MY_ENV_VARIABLE and assigns "foo" to it. Then the env utility runs bash, which takes over interpretation on the rest of the script. And now a historical note on the significance of the -S option, courtesy of the FreeBSD env manual page:
Quote:
Note that the way the kernel parses the `#!' (first line) of an interpreted script has changed as of FreeBSD 6.0. Prior to that, the FreeBSD kernel would split that first line into separate arguments based on any whitespace (space or <tab> characters) found in the line. So, if a script named /usr/local/bin/someport had a first line of:
Code:
#!/usr/local/bin/php -n -q -dsafe_mode=0
then the /usr/local/bin/php program would have been started with the arguments of:
plus any arguments the user specified when executing someport. However, this processing of multiple options on the `#!' line is not the way any other operating system parses the first line of an interpreted script. So after a change which was made for FreeBSD 6.0 release, that script will result in /usr/local/bin/php being started with the arguments of:
plus any arguments the user specified. This caused a significant change in the behavior of a few scripts. In the case of above script, to have it behave the same way under FreeBSD 6.0 as it did under earlier releases, the first line should be changed to:
and then -S processing will split that line into separate arguments before executing /usr/local/bin/php.
In other words, the "-S" is pretty important if you do anything more complicated than
Code:
/usr/bin/env bash
by passing options to env and/or bash. If the manual is correct, this should be available on many systems, but I only have BSD systems so I can't check it out for you.
I mean, the current environment is applied to the program that env is going to execute. And by passing VARIABLE=value options to env, you can modify that environment. For example:
MY_ENV_VARIABLE probably won't exist before the script starts executing. But when you call env, it creates the variable MY_ENV_VARIABLE and assigns "foo" to it. Then the env utility runs bash, which takes over interpretation on the rest of the script. And now a historical note on the significance of the -S option, courtesy of the FreeBSD env manual page:In other words, the "-S" is pretty important if you do anything more complicated than
Code:
/usr/bin/env bash
by passing options to env and/or bash. If the manual is correct, this should be available on many systems, but I only have BSD systems so I can't check it out for you.
I have tried your method on my Red Hat Linux system, but the behavior is quite different.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.