Home > Software design >  host vs inside container: bash command with awk returns different result
host vs inside container: bash command with awk returns different result

Time:11-17

command ran inside the container

root@foo:/# 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

[ec2-user@ip-x ~]$ 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

CodePudding user response:

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
  • Related