I am new to shell scripting in Linux, what is the meaning of a=$a $b [duplicate] - linux

test.sh:
#! /bin/sh
me=I ./test2.sh
test2.sh:
#! /bin/sh
echo $me
run script 1 and printing this:
[zhibin#szrnd1 sh]$ ./test.sh
I
[zhibin#szrnd1 sh]$
As see, the variable "$me" be transferred to "test2.sh".
I didn't find this usage of variable definition on googling, can someone tell me where can find the tutorial including the usage above mentioned?
Thx a lot!

Since this has been mentioned on SO a lot, I'm assuming you're looking for some documentation on it. I'm not sure there is anything more detailed than the BASH documentation about this:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.
As you've seen by experimenting, when you do "A=B command", it runs the command as if "export A=B" was run just prior to that command then A's value reverts to its previous after command completes. It's a very convenient way to pass some environment into a command while ensuring the rest of the script is not affected.

From the bash documentation on the Environment:
The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.
So if you put variable assignments before a command, that command is run with those environment variables.

Related

Difference between ways in script execution [duplicate]

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script] and bash [script] are equivalent, right??).
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
Here's the "experiment" I did:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.
bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)
$SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)
./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.
script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

What's the difference between "./<executable>" and ". ./<executable>"? [duplicate]

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script] and bash [script] are equivalent, right??).
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
Here's the "experiment" I did:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.
bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)
$SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)
./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.
script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

Difference between running a script and copying its content and running it on in a terminal [duplicate]

This question already has answers here:
Global environment variables in a shell script
(7 answers)
Closed 2 years ago.
I have a seemingly simple bash script to setup my environment:
The first two lines are:
#!/bin/bash
export CVE_ENV_DIR=$PWD
easy, hey? Well, see what happens when I run it, I get the following output:
$ echo $PWD
/work/env
$ ./env.sh
$ echo $CVE_ENV_DIR
$
Why does CVE_ENV_DIR not get set to /work/env? What is happening here? When I type export CVE_ENV_DIR=$PWD manually on the shell, it works as expected...
Child shells cannot affect the environment of their parent. If you want the script to affect the parent's environment, you need to:
source ./env.sh
So what's going on? When you run a bash script, either by bash env.sh or env.sh, you're spawning a program with its own environment, an environment that's divorced from its parent. But, when you run the commands contained in the script at the command line, or using source, there is no spawned environment.
Edit to address #syme's comment. Bash scripts meant to be read using source are often pure configuration, containing only assignments and calculations. It's possible to also make them a little more helpful and self-documenting with a clever she-bang hack like:
#!/bin/echo USAGE: source
# Default configuration file for the Frobnicator package.
FOO=bar
BAR=$(stat /baz)
[[ -f /baz ]] && BAZ=file || BAZ=
export FOO BAR BAZ
Making a bash script meant for configuration look like a configuration script, you help future maintainers. You also help yourself my modularizing your script code into distinct parts, each part with its one unique function.
As a side note, please don't export on the same line as you assign.

What's the difference between: ". [script]" or "source [script]", "bash [script] or $SHELL [script]", and "./ [script]" or "[script]"?

I know that source and . do the same thing, and I would be surprised to learn if the other pairs of commands in the title don't so the same thing (because I'm running bash as my shell, $SHELL [script] and bash [script] are equivalent, right??).
So what's the difference between the three methods of executing the script? I'm asking because I just learned that sourcing a script is NOT the exact same as executing it. In a way that I didn't find obvious from running my "experiments" and reading the man pages.
What are the other subtle differences that I couldn't find by blindly calling these functions on incredibly simple scripts that I've written? After reading the above-linked answer, I can strongly guess that the answer to my question will be quite a simple explanation, but in a way that I'd almost never fully discover by myself.
Here's the "experiment" I did:
$. myScript.sh
"This is the output to my script. I'd like to think it's original."
$source myScript.sh
"This is the output to my script. I'd like to think it's original."
$bash myScript.sh
"This is the output to my script. I'd like to think it's original."
$$SHELL myScript.sh
"This is the output to my script. I'd like to think it's original."
$./myScript.sh
"This is the output to my script. I'd like to think it's original."
$myScript.sh
"This is the output to my script. I'd like to think it's original."
. script and source script execute the contents of script in the current environment, i.e. without creating a subshell. On the upside this allows script to affect the current environment, for example changing environment variables or changing the current work directory. On the downside this allows script to affect the current environment, which is a potential security hazard.
bash script passes script to the bash interpreter to execute. Whatever shebang is given by script itself is ignored. ("Shebang" referring to the first line of script, which could e.g. read #!/bin/bash, or #!/usr/bin/perl, or #!/usr/bin/awk, to specify the interpreter to be used.)
$SHELL script passes script to whatever is your current shell interpreter to execute. That may, or may not, be bash. (The environment variable SHELL holds the name of your current shell interpreter. $SHELL, if running bash, is evaluated to /bin/bash, with the effect detailed in the previous paragraph.)
./script executes the contents of a file script in the current work directory. If there is no such file, an error is generated. The contents of $PATH have no effect on what happens.
script looks for a file script in the directories listed in $PATH, which may or may not include the current work directory. The first script found in this list of directories is executed, which may or may not be the one in your current work directory.

In my command-line, why does echo $0 return "-"?

When I type echo $0 I see -
I expect to see bash or some filename, what does it mean if I just get a "-"?
A hyphen in front of $0 means that this program is a login shell.
note: $0 does not always contain accurate path to the running executable as there is a way to override it when calling execve(2).
I get '-bash', a few weeks ago, I played with modifying a process name visible when you run ps or top/htop or echo $0. To answer you question directly, I don't think it means anything. Echo is a built-in function of bash, so when it checks the arguments list, bash is actually doing the checking, and seeing itself there.
Your intuition is correct, if you wrote echo $0 in a script file, and ran that, you would see the script's filename.
So based on one of your comments, you're really want to know how to determine what shell you're running; you assumed $0 was the solution, and asked about that, but as you've seen $0 won't reliably tell you what you need to know.
If you're running bash, then several unexported variables will be set, including $BASH_VERSION. If you're running tcsh, then the shell variables $tcsh and $version will be set. (Note that $version is an excessively generic name; I've run into problems where some system-wide startup script sets it and clobbers the tcsh-specific variable. But $tcsh should be reliable.)
The real problem, though, is that bash and tcsh syntax are mostly incompatible. It might be possible to write a script that can execute when invoked (via . or source) from either tcsh or bash, but it would be difficult and ugly.
The usual approach is to have separate setup files, one for each shell you use. For example, if you're running bash you might run
. ~/setup.bash
or
. ~/setup.sh
and if you're running tcsh you might run
source ~/setup.tcsh
or
source ~/setup.csh
The .sh or .csh versions refer to the ancestors of both shells; it makes sense to use those suffixes if you're not using any bash-specific or tcsh-specific features.
But that requires knowing which shell you're running.
You could probably set up an alias in your .cshrc, .tcshrc, or.login, and an alias or function in your.profile,.bash_profile, or.bashrc` that will invoke whichever script you need.
Or if you want to do the setup every time you login, or every time you start a new interactive shell, you can put the commands directly in the appropriate shell startup file(s). Of course the commands will be different for tcsh vs. bash.

Resources