home about me

The difference between " and ' in shell aliases

Not doing clickbait: Single quotes delay command substition, double quotes do it immediately.

Let’s take this example:

alias date-single='echo $(date)'
alias date-double="echo $(date)"

See how our trusty code highlighting already hints at the difference?

Now date-single will always execute echo $(date), and thus echo the current time. date-double on the other hand is populated at time of initialization, and will always echo the time the alias was defined.

Using the alias command to list our aliases, we get those results:

date-double='echo Sun Jan 27 16:01:07 CET 2019'
date-single='echo $(date)'

Double quotes if you only need command substitution at initialization time, single quotes if you need your subshells at runtime.