I have a command:

synclient -l | awk -v param_name=$param_name '$1==param_name {print $(NF)}'

and its output is just: 0.

But if I replace both ' with ", I'm getting:

Command 'NF' not found, but can be installed with:
sudo apt install byobu
awk: cmd. line:1: ==param_name {print }
awk: cmd. line:1: ^ syntax error

The same error appears when I use sh -c on the commands:

sh -c "synclient -l | awk -v param_name=$param_name '$1==param_name {print $(NF)}'"
sh -c 'synclient -l | awk -v param_name=$param_name "$1==param_name {print $(NF)}"'

What's the difference between ' and " here? Why does it happen?


1 Answer 1


From bash manual:

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. [...]

Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !.

Double quotes is clearly the wrong method here.

In your case, the $ is is not preserved for awk, but evaluated by bash, which leads to 2 errors.

  • bash expands $1 which is empty in your case, that is why awk complains about a syntax error.
  • $(NF) is command substitution, so bash tries to run a command with name NF, which is not available.

Also, you should double quote $param_name, otherwise it will lead to an error, if it contains space.

-v param_name="$param_name"
  • I'd like to use the code as a part of a custom shortcut but adding the double quote stops it from working completely. more
    – maciejwww
    Apr 27 at 12:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .