0

In the question How to execute ssh-keygen without prompt I find they use the here string like this:

""$'\n'"y"

Why do they need double quote before $? It seems $'\n'y is equal to it:

bash$ cat - <<< ""$'\n'"y" | od -c
0000000 \n y \n
0000003
bash$ cat - <<< $'\n'y | od -c
0000000 \n y \n
0000003 
tripleee
  • 175,061
  • 34
  • 275
  • 318
leopay
  • 21
  • 3
  • 2
    I've edited the linked answer to remove the wholly unnecessary `""`. – chepner Jul 30 '21 at 17:20
  • @chepner thanks, I'm not sure if they have any special meaning in double quotes – leopay Jul 30 '21 at 17:28
  • They have none. Quotes just perform bulk escaping: `"foo"` is the same as `\f\o\o`. If there's nothing in the quotes to escape, they are unnecessary. – chepner Jul 30 '21 at 17:30

1 Answers1

3

There is no reason; the empty double-quoted string is completely unnecessary.

The shell uses quoting to group tokens which would otherwise be split. token is precisely equivalent to ""token or "token" (or """"""""""token or """t"o"k"e"n" etc if you will) after quote removal.

The only place you really need an empty string is when an argument should be a string of length zero (like printf '%s\n' "first line" "" "third line" where the penultimate argument is an empty string) though it can also occasionally be useful for disambiguation ("$HOMEdir" is distinct from "$HOME""dir" where the former attempts to expand a variable whose name is seven characters long; though usually you'd use "${HOME}dir" to make this distinction).


(Multiple adjacent quotes have no significance to the shell; """" is simply the empty string "" next to the empty string "".)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Some users erroneously try to use quotes for disambiguation where they do not disambiguate at all. `echo "-e"` is precisely equivalent to `echo -e` and does not allow you to output the literal string `-e` (finally learn to use `printf '%s\n' -e` instead if you need that). – tripleee May 29 '23 at 04:56