Setting environment variable for one program call in bash using env

后端 未结 4 1816
失恋的感觉
失恋的感觉 2020-12-29 02:41

I am trying to invoke a shell command with a modified environment via the command env.

According to the manual

env HELLO=\'Hello World\'         


        
4条回答
  •  温柔的废话
    2020-12-29 02:47

    I think what happens is similar to this situation in which I was also puzzled.

    In a nutshell, the variable expansion in the first case is done by the current shell which doesn't have $HELLO in its environment. In the second case, though, single quotes prevent the current shell from doing the variable expansion, so everything works as expected.

    Note how changing single quotes to double quotes prevents this command from working the way you want:

    HELLO='Hello World' bash -c "echo $HELLO"
    

    Now this will be failing for the same reason as the first command in your question.

提交回复
热议问题