문제

$ setenv a "hi"

$ echo $a

hi

Similarly, when I do the below :

$ setenv b "hi" | grep "hi"

$ echo $b

b: Undefined variable.

Why is the setting lost?

My actual usage is source a.csh | grep "bla" ; echo $a (where $a is set in a.csh), but I thought the above is a simplistic case.

도움이 되었습니까?

해결책

From the man page:

If any component of a pipeline except the last is a builtin command, the pipeline is executed in a subshell.

So the variable is set in a subshell only.

다른 팁

In your case source a.csh | grep "bla" ; echo $a (where $a is set in a.csh) the shell tries to expand the variable $a before actually executing the whole command so it is still not set by source a.csh.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top