Issue
command ran inside the container
[email protected]:/# for pid in `ps -ax|awk '{print $1}'` ;do echo $pid; done
PID
1
17
18
34
1792
2952
3623
3649
3650
3651
however when i run the same command from host
[[email protected] ~]$ docker exec b1b bash -c "for pid in `ps -ax|awk '{print $1}'` ;do echo $pid; done"
bash: -c: line 1: syntax error near unexpected token `1'
bash: -c: line 1: `1'
i tried escaping $
in the aws based on here without any luck
docker exec b1b bash -c "for pid in `ps -ax|awk \"{print \$1}\"`;do echo $pid; done"
bash: -c: line 1: syntax error near unexpected token `1'
bash: -c: line 1: ` 1 ? Ss 10:56 /usr/lib/systemd/systemd --system --deserialize 21'
i was expecting the command to return same result when running inside the container vs running from host
Solution
Just use '' for bash -c and it will work correctly:
docker exec b1b bash -c 'for pid in `ps -ax|awk "{print \\$1}"`;do echo $pid; done'
example:
# docker exec 06 bash -c 'for pid in `ps -ax|awk "{print \\$1}"`;do echo $pid; done'
PID
1
88
93
94
95
Answered By - Marek Knappe Answer Checked By - Senaida (WPSolving Volunteer)