How to escape commands in a bashrc alias?
Solution 1:
The shell expands the command line containing the alias
command and passes something like td=touch 2010-09-17_21-54.txt
to the alias
command. You need to protect the special characters in the alias definition from expansion. The easiest way is to use single quotes instead of double quotes:
alias td='touch `date "+%Y-%m-%d_%H-%M"`.txt'
Then td
is an alias for touch `date "+%Y-%m-%d_%H-%M"`.txt
as desired.
Although it's not an issue here, I recommend using $(…)
instead of `…`
, so as to avoid difficulties with complex commands (backquotes have arcane and nonportable quoting rules, whereas dollar-parenthesis works intuitively):
alias td='touch $(date "+%Y-%m-%d_%H-%M").txt'