Bash - Assigning existing Variable values to a New Variable. New Variable values does not return the assigned value
You aren't defining a new variable named MyVar
. You are appending the empty string resulting from the expansion of the non-existent variable to the value of VAR1
.
You want
MyVar=$VAR1
to get the desired result.
An example of what +=
does do:
$ x=foo
$ echo "$x"
foo
$ x+=bar
$ echo "$x"
foobar
Unix variable assignment inside shell
Here is the answer :
f=timestep
timestep=$(cut -d' ' -f1 $f)
echo "$timestep"
Variable assignment convention in shell scripts
For specific shells, you'll need to look up the documentation for that shell. For bash
, it's a requirement that there be no spaces in that form (note that there are no spaces on either side of the =
):
A variable may be assigned to by a statement of the form
name=[value]
.
One reason why this may be the case is that you can use command-temporal assignments where a variable is set only for the duration of a command:
pax:~> x=314159 ; echo $x
314159
pax:~> x=42 bash -c 'echo $x'
42
pax:~> echo $x
314159
Allowing spaces in the assignment would make it a little difficult to figure out where the assignment finished and where the command started. For example:
x= echo echo hello
Should this set x
to echo
then run echo hello
or should it set x
to ""
and then run echo echo hello
?
If you want your assignment to be nicely formatted with spaces, you can use the ((...))
arithmetic evaluation:
pax:~> (( val = 117 / 3 )) ; echo $val
39
Bash variable declaration vs assignment
The function GetValue
along with printing a string to stdout also returns a code 1
. So assignment to the variable var1
from the function, though sets the value to foobar
, sets an exit code 1
which is a non-zero code causing the part after the ||
to be invoked.
But remember the same happens in the case with the declare
, but the reason you see Successed
is because the exit code 1
from the function is suppressed when the declare
built-in acts on the variable. So
var2="$(GetValue)" # sets a non-zero exit code 1
declare var2 # sets a success exit code 0
Since the last foreground command's exit code is the one used in the evaluation of success/failure, the part after &&
, which is echo "Successed!"
gets invoked.
So, your claim of using declare
in the variable assignment makes a difference in the exit code is incorrect, because these two still remain the same as they both set the exit code 1
which triggers the false case.
var1="$(GetValue)" && echo "Successed!" || echo "Failed!";
var2="$(GetValue)" && echo "Successed!" || echo "Failed!";
Suggest to always separate the initialization/declaration when using the built-ins local
, declare
and export
var=5
declare -i var
is preferred over declare -i var=5
.
See Why does local
/declare
sweep the return code of a command? for more reference on the subject.
bash shell script variable assignment
You can't have spaces between your variable name and the assignment operator =
. If you do it will treat the variable name as a command.
Try:
len=${#servers[@]}
What does `;` and `&&` do to variable assignment expressions in Bash?
There are two types of vars: Environment (exported) vars, and shell vars.
VAR=val
sets the env var namedVAR
if it already exists.VAR=val
sets the shell var namedVAR
if env varVAR
doesn't exist.VAR=val cmd
sets the env var namedVAR
forcmd
only.
The env vars are provided to child processes as their environment.
Shell vars, on the other hand, exist only within that shell process. Child processes have no access to them.
So,
- You set env var
TEST
for the command. - You set shell var
TEST
. - You set shell var
TEST
. - You set shell var
TEST
. Then you set env varTEST
for the command. - You env shell var
TEST
. Then you set env varTEST
for the command.
Why does #4 work?
You set an env var.
Why does not #3 work?
You did not set any env vars.
How to assign echo to a variable in Bash?
You can use $()
(command substitution):
test=$(echo "foo=bar" | cut -d "=" -f1)
echo "$test"
foo
Bash unable to assign value
I think you want:
data1="$(wc -l data.txt | awk '{ print $1 }')"
The $()
syntax causes bash to execute that expression and replace it with the results.
Actually, powershell does allow you to do a straight =
assignment like you did...
Related Topics
How Would I Tell a Bash Script to Start Over from the Top
Where Is the Stack Memory Allocated from for a Linux Process
Assigning Dynamic Bash Variable Names Using a for Loop Seq
How to Pass Arguments with Special Characters to Call Shell Script
Why Ulimit Can't Limit Resident Memory Successfully and How
How to Grep for Value in a Key-Value Store from Plain Text
Rsync Over Ssh Preserve Ownership Only for Www-Data Owned Files
Invalid Argument" Setting Key "Net.Core.Somaxconn"
Intellij Idea Under Linux, No Such File or Directory on Main Class
Append The Time Stamp to a File Name in Ubuntu
How to Get Ctrl-Backspace to Delete a Word in Vim Within Gnome-Terminal
Adding Support for Menuconfig/Kconfig in My Project
Smbclient - Send All Files in Directory
Replace Text Between Two Strings in File Using Linux Bash