2

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.

Pavan
  • 163
  • 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 '19 at 12:49

1 Answers1

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.