Assign and use of a variable in the same subshell

前端 未结 3 1496
有刺的猬
有刺的猬 2020-12-11 17:39

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 availa

3条回答
  •  暖寄归人
    2020-12-11 18:05

    Put simply, the "$v" is evaluated before the command is called while prepending "v=5" in front of the command modifies the environment of the command you're running.

    As Charles Duffy said, you can add an intermediate 'sh' process that will evaluate the variable with a similar syntax but you probably want to do something a bit more elaborate and it'd be useful to know what if you still have troubles with it.

提交回复
热议问题