Assign and use of a variable in the same subshell

806 views Asked by At

I was doing something very simple like: v=5 echo "$v" and expected it to print 5. However, it does not. The value that was just set is not available for the next command.

I recently learnt that "In most shells, each command of a pipeline is executed in a separate SubShell". However, in this case both commands are being executed in the same subshell.

Why does this happen? Is there any way to have this working?

Full example:

$ v=1
$ v=5 echo "$v"
1                   # I expected 5!
0

There are 0 answers