I want to send a string that contains the newline character as a command line argument to my program, the following works:

myprogram Hello$'\n'World

But when I use double quotes on the string:

myprogram "Hello$'\n'World Hello Again"

Then the string received by myprogram will contain the string $'\n' and not the newline character.

  • Once you use quotes you can just enter a literal newline.
    – user601
    Jan 2 '18 at 19:39

The $'…' construct doesn't work if quoted. Use:

$ myprogram "Hello"$'\n'"World Hello Again"


$ myprogram Hello$'\n'World\ Hello\ Again


$ a=$'\n'
$ myprogram "Hello${a}World Hello Again"


$ myprogram "$(echo -e 'Hello\nWorld Hello Again')"


$ myprogram "Hello
World Hello Again"
  • 1
    Bash's manual also lists $'...' as a form of quoting, even though the dollar sign makes it look a bit like many other expansions.
    – ilkkachu
    Jan 2 '18 at 18:10
  • @ilkkachu Last line of the page you link to There are three quoting mechanisms: the escape character, single quotes, and double quotes. So, ANSI-C quoting is not a quoting mechanism, it is something else.
    – ImHere
    Jan 2 '18 at 18:36
  • @ilkkachu is right: extquote - If set, $'string' and $"string" ***quoting*** is performed within ... [blah blah blah] The bash man page is very inconsistent; it is a usage manual NOT a computer science treatise. If you doubt me, have a look through some of Tim's questions about it.
    – Wildcard
    Jan 3 '18 at 6:27
  • Yes, that's a somewhat confusing sentence since the page lists five types of well, things. Though amusingly, exactly three of them are named quotes or quoting, just not the same ones. Time to start writing bug reports and patches for the documentation?
    – ilkkachu
    Jan 3 '18 at 8:01
  • @Wildcard Yes, the result of $'…' expansion is considered quoted but it is not a way to convert to quoted characters that are unquoted.
    – ImHere
    Jan 3 '18 at 11:40


myprogram "$(printf '%s\n' "Hello" "world once" "World Hello Again")"

The printf command will re-use the format string to consume all the arguments given. Also, the $() syntax will automatically remove any trailing newlines.


The string doesn't need to be contained in one all-encompassing pair of quotes; individually quoted strings can be concatenated into one word.

myprogram Hello$'\n'"World Hello Again"

Quotes don't define a string; they simply escape every character that occurs between them.


Interesting that in three answers, no one has pointed out the simplest solution:

myprogram $'Hello\nWorld Hello Again'

Since dollar quoting is (arguably) a form of quoting, just use that for the whole argument.

Of course, it is certainly worthwhile to understand that quotes are NOT delimiters of any sort in bash. White space is used for word splitting—quotes do not delimit the point where one argument ends and another begins. This means you can freely mix quoting methods in a single argument. (There is a good description of this elsewhere on this site already; I will see if I can find it and link to it.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.