How to escape commands in a bashrc alias?
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping
--
Chapters
00:00 How To Escape Commands In A Bashrc Alias?
00:30 Accepted Answer Score 8
01:21 Answer 2 Score 1
01:34 Thank you
--
Full question
https://superuser.com/questions/189904/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #bashrc
#avk47
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping
--
Chapters
00:00 How To Escape Commands In A Bashrc Alias?
00:30 Accepted Answer Score 8
01:21 Answer 2 Score 1
01:34 Thank you
--
Full question
https://superuser.com/questions/189904/h...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #bashrc
#avk47
ACCEPTED ANSWER
Score 8
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'
ANSWER 2
Score 1
The accepted answer is perfect almost always. For those times when you MUST use double-quotes instead of single-quotes, the following works:
alias td="touch \`date \"+%Y-%m-%d_%H-%M\"\`.txt"