Setting an environment variable before a command in Bash is not working for the second command in a pipe

前端 未结 6 1501
故里飘歌
故里飘歌 2020-11-27 08:54

In a given shell, normally I\'d set a variable or variables and then run a command. Recently I learned about the concept of prepending a variable definition to a command:

6条回答
  •  庸人自扰
    2020-11-27 09:38

    How about exporting the variable, but only inside the subshell?:

    (export FOO=bar && somecommand someargs | somecommand2)
    

    Keith has a point, to unconditionally execute the commands, do this:

    (export FOO=bar; somecommand someargs | somecommand2)
    

提交回复
热议问题