문제

Please explain this problem with incrementing value, we have next file named test.sh

#!/bin/bash

ps aux | grep test.sh -c
echo $(ps aux | grep test.sh -c)

and then run it

$ ./test.sh
2
3

I know there is two lines after grep slice (1 with test.sh, 2 with grep), why 3 come in? Thanks

도움이 되었습니까?

해결책

You get 3 in the second case because the second command $(...) (i.e. command substitution) executes in a subshell.

From the manual:

Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, ...

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