Here is my script:
echo "Defining aliases"
echo $$
echo "
s=screen
v=vlock -a
j=jobs
" | sed '/^$/d' | while read alias
do
echo $$
alias "$alias"
alias | wc -l
done
And here is its output:
parthian@computer$ . aliases
Defining aliases
3744
3744
4
3744
5
3744
6
parthian@computer$ ps -p $$
PID TTY TIME CMD
3744 pts/1 00:00:03 bash
parthian@computer$ alias | wc -l
3
I'm flummoxed. This is some of the most straightforward code I've ever written, and it's not doing what it's supposed to do.
EDIT: When I change the echo lines from echo $$
to echo $$ $BASHPID
, the second PID ($BASHPID
) is different inside the loop, but $$
remains the same throughout. Could this be the issue?
Best Answer
Don’t believe everything a computer tells you.
When you do
some_command | while …
, the shell puts thewhile
loop into a subshell, but$$
continues to report the PID of the main (parent) process. In bash,$BASHPID
reveals the truth.Consider doing