我很难理解|和>运算符之间的区别
我看过的地方有:
但对这些解释没有足够的意义。
以下是我的实际例子:
测试-a.sh:
alias testa='echo "alias testa here"'
echo "testa echo"
echo "testa echo2"测试-b.sh:
alias testb='echo "alias testb here"'
echo "testa echo"
echo "testa echo2"test-pipes.sh:
function indent() {
input=$(cat)
echo "$input" | perl -p -e 's/(.*)/ \1/'
}
source test-a.sh | indent
testa
source test-b.sh > >(indent)
testb产出:
$ source test-pipes.sh
testa echo
testa echo2
test-pipes.sh:10: command not found: testa
testa echo
testa echo2
alias testb here管道不允许在当前进程中设置别名,但重定向允许设置别名。
有人能给出一个简单的解释吗?
发布于 2018-01-11 23:37:30
来自bash手册页
管道中的每个命令都作为单独的进程执行(即在子shell中)。
子进程所做的许多事情都是与父进程隔离的。这些列表包括:更改当前目录、设置shell变量、设置环境变量和别名。
$ alias foo='echo bar' | :
$ foo
foo: command not found
$ foo=bar | :; echo $foo
$ export foo=bar | :; echo $foo
$ cd / | :; $ pwd
/home/jkugelman注意这些更改是如何没有生效的。使用显式子see可以看到同样的情况:
$ (alias foo='echo bar')
$ foo
foo: command not found
$ (foo=bar); echo $foo
$ (export foo=bar); echo $foo
$ (cd /); pwd
/home/jkugelman另一方面,重定向不创建子subshells。它们只是改变命令的输入和输出的位置。函数调用也是如此。函数在当前shell中执行,没有子shell,因此它们能够创建别名。
https://stackoverflow.com/questions/48217355
复制相似问题