2019-12-07 Dollar-quoted strings in Bash

As we all know, bash is a terrible but ubiquitous thing. Nowadays I usually avoid writing bash scripts as much as I can – but sometimes I really need to do something bash-y, for instance in the command line. One thing I encountered some time ago is putting newlines into bash strings, e.g. in parameters. My use case was that I wanted to make a Git commit from command line with a multiline commit message.

It turns out that it is possible. (One way is to give the -m option to git commit more than once, but this is translated to multiple paragraphs, not lines, i.e., Git puts two newlines between parts corresponding to separate -m arguments.) The bash syntax for that is $'...', i.e., single-quote strings preceded by a dollar sign. In such a string you can use the usual backslash-character escape sequences (like \n standing for a newline), and also some other constructs – see the manpage for the details.

Interestingly, dollar-prefixed double quote strings are a completely different thing, as I learned from this answer on Unix & Linux StackExchange.

CategoryEnglish, CategoryBlog