<-- back

Bash. Variables inside aliases

Hey,

Recently I had to use bash alias that contains $(pwd) inside so I’ve defined it like that:

alias thename="something $(pwd) --option=value"


Of course I know a bit of bash so that’s why I use double quotes!

how I feel

But it turned out to be wrong. Each time when my bash/zsh was opened the alias was defined with INTERPRETED $(pwd) which is the directory where bash was opened (usualy home dir).

Actually we have to use single quotes to make sure that $(pwd) isn’t interpreted at the moment of alias definition but it is when we call the alias.

Summary


  • use single quotes if you want to make sure variable/command is interpreted during the alias call.
  • use double quotes if you only want to interpretate variable/command on the alias definition stage (and only once).

Feel free to contact me for feedback or questions. Find my contacts on About page.