1

This question already has an answer here:

I was trying to create an alias in bash

alias r='!!'

My idea was to create a simple alias r to run the previous command instead of using !! (double exclamation) to run the most recent command as it is hard to type. I know I can alias r='fc -s' to make this work, but I was trying to understand why I am not able to create an alias for !!. I want to understand what !! means to bash and why I'm not able to alias it. I can create alias for other bash builtin commands like alias cdir='cd' but then why not for !! Sorry if I couldn't explain properly.

marked as duplicate by muru, pLumo, Gilles bash Jul 10 at 13:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I do not think this is a true and absolute answer, so I am posting it as a comment: According to tldp, since ! is a special char (to negate in scripts or invoke history on the command line), it is disabled in scripts. I think that behavior is disabling it in the bash alias function. According to the appendix L, !! is an internal variable and "Unfortunately, the Bash history tools find no use in scripting." – number9 Jul 10 at 12:49
1

The history expansion in bash (of which the !! is part of) happens before the line is split into words and aliases are expanded. Consequently, there is no way to use any history expansion in aliases or functions.

This is different from the original in csh.

  • Thanks Billy, this cleared my doubt. – Pavan Jul 11 at 9:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.