Score:0

The difference between ' and "

cn flag

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?

pLumo avatar
in flag
See https://www.gnu.org/software/bash/manual/html_node/Quoting.html
sudodus avatar
jp flag
A variable `'$param'` within single quote characters is not expanded, and this happens in both cases in your original question. Use only double quote characters around variables `"$param"`.
Score:2
in flag

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"
maciejwww avatar
cn flag
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](https://askubuntu.com/questions/1465502/how-to-use-a-bash-command-as-a-custom-shortcut)
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.