Scenario
I'm trying to the following code to make sure that only one of this bash is running at a time:
this_bash=$(basename $0)
this_pid=${$}
is_running="$(pidof -x $this_bash -o $this_pid | wc -l )"
I found it always returns 1, even when there is no any bash with same name running.
Investigation
For further examination I tried this:
z=$(pidof -x $this_bash -o $this_pid)
echo "[$z]"
echo "[$(pidof -x $this_bash -o $this_pid)]"
echo "[$($z | wc -l )]"
echo "[$(pidof -x $this_bash -o $this_pid | wc -l )]"
the square brackets are to make sure there are no hidden white space chars.
The result was:
[]
[]
[0]
[1]
Question
I don't understand why storing pidof as variable returns the expected result, while piping the commands directly does not.