I've found some anomaly when writing a script.
The following examples works as expected:
$ echo 123 | awk '{print $1 456}'
123456
$ sh -c "echo 123 | awk '{print $1}'"
123
But the following example, doesn't:
$ sh -c "echo 123 | awk '{print $1 456}'"
456
I'm expecting to print the 1st column with the additional string, which should return 123456 as it does when running the same command withoutsh -c. But what is happening, the 1st column is ignored for some reason. What's interesting, $1 is printed without problems when not performing string concatenation.
Why this is happening and how to do string concatenation within the command which is passed to separate instance of shell?
sh -c "echo 123 | awk '{print $1}'", is “working for the wrong reason” – it’s not doing what you think it’s doing. Trysh -c "echo 123 456 | awk '{print $1}'"– it will print123 456, because the$1gets replaced by the current value of$1in the shell (which is nothing) early in the interpretation of the command, soawkgets the command string{print }, causing it to print every line in its entirety, not just the first column.