DEV Community

Eda
Eda

Posted on

A reminder to use single quotes when writing commit messages

Originally published at https://rivea0.github.io/blog.

Using backticks in the shell results in command expansion, for example:

echo `which cp`
Enter fullscreen mode Exit fullscreen mode

is the same as

echo $(which cp)
Enter fullscreen mode Exit fullscreen mode

It finds the directory the cp command is in (executes the which command), and then prints it to the screen.

Normally, when using double quotes, many types of expansions do not work (like word-splitting and brace expansions), but with command expansion, this is not the case.

Commands inside backticks are executed if they are inside double quotes.

When writing a message to git commit, rendering the name of a function in monospace, for example, is easy to do using backticks.

So, my hypothesis is that when I do something like

git commit -m "Add `doThis` function"
Enter fullscreen mode Exit fullscreen mode

If doThis is a command, it will be executed.

So, if we write something like

git commit -m "Add `which cp` thing"
Enter fullscreen mode Exit fullscreen mode

It will replace it with the directory that cp is in, so that our commit message will look like:

Add /bin/cp thing
Enter fullscreen mode Exit fullscreen mode

In fact, after trying it, it turns out to be true, and this Stack Overflow answer has a nice short explanation.

Single quotes, on the other hand, suppress all expansions, so if written like this:

git commit -m 'Add `which cp` thing'
Enter fullscreen mode Exit fullscreen mode

It won't execute which cp, but write it as it is:

Add `which cp` thing
Enter fullscreen mode Exit fullscreen mode

So, I think it is safer to use single quotes, whether you want to use a code block a la markdown styling or not.


This enlightenment is thanks to the "Seeing the World as the Shell Sees It" chapter from William E. Shotts' book The Linux Command Line: A Complete Introduction.

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.