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:
在给定的shell中,通常我会设置一个变量或变量,然后运行一个命令。最近,我了解了对一个命令进行变量定义之前的概念:
FOO=bar somecommand someargs
This works... kind of. It doesn't work when you're changing a LC_* variable (which seems to affect the command but NOT its arguments, e.g., '[a-z]' char ranges) or when piping output to another command thusly:
这工作…种。当您更改一个LC_*变量时(这似乎会影响到命令,而不是它的参数,例如“[a-z]”字符范围),或者当管道输出到另一个命令时,它是无效的:
FOO=bar somecommand someargs | somecommand2 # somecommand2 is unaware of FOO
I can prepend somecommand2 with "FOO=bar" as well, which works but which adds unwanted duplication, and it doesn't help with arguments that are interpreted depending on the variable (e.g. '[a-z]')
我还可以用“FOO=bar”来预判somecommand2,它可以工作,但是会添加不必要的重复,并且它不会帮助根据变量来解释参数(例如:“[a - z]”)
So, what's a good way to do this on a single line? I'm thinking something on the order of:
那么,在单行上做这个的好方法是什么呢?我在想:
FOO=bar (somecommand someargs | somecommand2) # Doesn't actually work
Edit: I got lots of good answers! The goal is to keep this a one-liner, preferably without using "export". The method using a call to bash was best overall, though the parenthetical version with "export" in it was a little more compact. The method of using redirection rather than a pipe is interesting as well.
我有很多好的答案!我们的目标是让这一行代码保持不变,最好不要使用“导出”。使用调用bash的方法总体上是最好的,不过带有“导出”的圆括号版本稍微紧凑一些。使用重定向而不是管道的方法也很有趣。
4 个解决方案
#1
211
FOO=bar bash -c 'somecommand someargs | somecommand2'
#2
142
How about exporting the variable, but only inside the subshell?:
如何导出变量,但只在子shell中?
(export FOO=bar && somecommand someargs | somecommand2)
Keith has a point, to unconditionally execute the commands, do this:
Keith有一点,无条件地执行命令,这样做:
(export FOO=bar; somecommand someargs | somecommand2)
#3
23
You can also use eval
:
你也可以使用eval:
FOO=bar eval 'somecommand someargs | somecommand2'
Since this answer with eval
doesn't seem to please everyone, let me clarify something: when used as written, with the single quotes, it is perfectly safe. It is good as it will not launch an external process (like the accepted answer) nor will it execute the commands in an extra subshell (like the other answer).
由于这个答案似乎并不是让所有人都满意,让我来澄清一些事情:当用单引号来写的时候,它是绝对安全的。它很好,因为它不会启动一个外部进程(就像被接受的答案一样),也不会在一个额外的子shell中执行命令(就像其他的答案一样)。
As we get a few regular views, it's probably good to give an alternative to eval
that will please everyone, and has all the benefits (and perhaps even more!) of this quick eval
“trick”. Just use a function! Define a function with all your commands:
当我们得到一些常规的视图时,最好给出一个能让每个人都满意的替代方法,并拥有这个快速eval“技巧”的所有优点(甚至更多)。只使用一个函数!用你所有的命令来定义一个函数:
mypipe() {
somecommand someargs | somecommand2
}
and execute it with your environment variables like this:
并使用这样的环境变量来执行它:
FOO=bar mypipe
#4
-5
How about using a shell script?
使用shell脚本怎么样?
#!/bin/bash
# myscript
FOO=bar
somecommand someargs | somecommand2
> ./myscript
#1
211
FOO=bar bash -c 'somecommand someargs | somecommand2'
#2
142
How about exporting the variable, but only inside the subshell?:
如何导出变量,但只在子shell中?
(export FOO=bar && somecommand someargs | somecommand2)
Keith has a point, to unconditionally execute the commands, do this:
Keith有一点,无条件地执行命令,这样做:
(export FOO=bar; somecommand someargs | somecommand2)
#3
23
You can also use eval
:
你也可以使用eval:
FOO=bar eval 'somecommand someargs | somecommand2'
Since this answer with eval
doesn't seem to please everyone, let me clarify something: when used as written, with the single quotes, it is perfectly safe. It is good as it will not launch an external process (like the accepted answer) nor will it execute the commands in an extra subshell (like the other answer).
由于这个答案似乎并不是让所有人都满意,让我来澄清一些事情:当用单引号来写的时候,它是绝对安全的。它很好,因为它不会启动一个外部进程(就像被接受的答案一样),也不会在一个额外的子shell中执行命令(就像其他的答案一样)。
As we get a few regular views, it's probably good to give an alternative to eval
that will please everyone, and has all the benefits (and perhaps even more!) of this quick eval
“trick”. Just use a function! Define a function with all your commands:
当我们得到一些常规的视图时,最好给出一个能让每个人都满意的替代方法,并拥有这个快速eval“技巧”的所有优点(甚至更多)。只使用一个函数!用你所有的命令来定义一个函数:
mypipe() {
somecommand someargs | somecommand2
}
and execute it with your environment variables like this:
并使用这样的环境变量来执行它:
FOO=bar mypipe
#4
-5
How about using a shell script?
使用shell脚本怎么样?
#!/bin/bash
# myscript
FOO=bar
somecommand someargs | somecommand2
> ./myscript