有没有办法设置一个变量,然后在整个管道bash命令中重复使用它,例如
> set v <VALUE> | comm1 v | comm2 v
I am not sure what you expect to flow through the first pipe but I guess the following solves your problem anyway. Asuming you use bash
:
v=<VALUE>; comm1 $v | comm2 $v
Note that around =
no spaces are allowed.
The way you seem to have described the problem, no. If you assign a value just before a command, that value gets exported to the command's environment, but doesn't affect the current (shell's) environment.
$ echo $x
$ x=123 bash -c 'echo $x'
123
$ echo $x
$
When you use a pipe you're creating subshells of the current shell, so only names assigned in the current shell and subsequent subshells will be recognized.
$ a=123; echo wat | echo $a
123
$ # ^ subshell ^
Of course, it's easy to do this if you just do the assignments as a separate step, as in…
a=123; foo | ( b=123; bar ) | baz …
but you need to manually wrap the subshell so the assignment becomes part of each pipe chunk.
Alternatively you can just create full shells as children:
a=123 bash -c 'echo $a | bar | baz'
But I can't think of any way in which that's a better approach than just assigning values when you need them.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.