I want to get the pids of two background processes,
sleep 20 & pid1=$\!; sleep 10 & pid2=$\!; echo "pid1: $pid1, pid2: $pid2"
and get output like below:
[1] 124646
[2] 124648
pid1: $!, pid2: $!
the output I desired to get is like:
pid1: 124646, pid2: 124648
Anyone know why and can help to achieve this?
[add 2018/01/02]
Sorry for really late response, one hand is busy, another is that I want to verify the script.
The actual script I want to run is like below:
sh -c "sleep 20 & pid1=$!; sleep 10 & pid2=$!; echo \"pid1: $pid1, pid2: $pid2\""
and as it will report bash: !: event not found
, so I tried to add \ and become:
sh -c "sleep 20 & pid1=$\!; sleep 10 & pid2=$\!; echo \"pid1: $pid1, pid2: $pid2\""
and for make the problem simple, I just rmeove sh -c
while this make it a quite different problem.
for my problem, I found out that below script will work:
sh -c 'sleep 20 & pid1=$!; sleep 10 & pid2=$!; echo "pid1: $pid1, pid2: $pid2"'
Yet there is another question, how to make below script to work:
name='bob'
# below script reports 'bash: !: event not found' error
sh -c "echo $name; sleep 20 & pid1=$!; sleep 10 & pid2=$!; echo \"pid1: $pid1, pid2: $pid2\""
# below script $name will not become bob
sh -c 'echo $name; sleep 20 & pid1=$!; sleep 10 & pid2=$!; echo \"pid1: $pid1, pid2: $pid2\"'