Assign and use of a variable in the same subshell

前端 未结 3 1501
有刺的猬
有刺的猬 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:21

    v=5 echo $v
    

    will add v=5 into the environment variables and then execute echo $v. The $v refers to the shell variable v which you have set to 1 in the first line.

    Adding the semi colon v=5;echo $v sets the shell variable to 5 and then executes the command after the semi-colon ie echo $v and produces 5.

    Try this command:

    v=5 env|less
    

    and look at the environment.

提交回复
热议问题