2

I was playing with string substitution while learning a bit more of bash, and I have add !! into some dummy example echo ${TEST/hey/!!}...

I was surprised that !! was replaced with last entered command... what is it? is it somehow connected to magick codes link $? or $_ or even -. Is there anything else I can use in same manner in bash?

Oleg Butuzov
  • 121
  • 3
  • 5
    Related (but doesn’t address the relationship with parameter expansion): [Understanding the exclamation mark (!) in bash](https://unix.stackexchange.com/q/3747/86440). – Stephen Kitt Dec 28 '18 at 09:46
  • 2
    They're not related, and they're not performed at the same stage (even if `$_` and `!$` will get you the same with simple commands). `!` will get you the "bang" / history expansion, a mechanism inherited from `csh`. Go read the "HISTORY EXPANSION" chapter from the bash manual. –  Dec 28 '18 at 10:03

1 Answers1

1

Bash performs different kinds of automatic replacements called expansions. For example, some of them are:

  • History Expansion
  • Path Expansion
  • Parameter Expansion
  • and others...

When you include !! bash automatically replaces by previous executed command. The character ! performs history expansion. For example !10 is replaced by the 10th command in the command history. $ does parameter and variable expansion. All of those characters: -, _, and ? are special parameter for bash.

The best source to learn about it is the bash manual: Bash Reference Manual: Top

The example you mention echo ${TEST/hey/!!} include both parameter and history expansions.

sebelk
  • 4,209
  • 10
  • 35
  • 54