Question

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

Was it helpful?

Solution

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, ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top