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?

  • 5
    Related (but doesn’t address the relationship with parameter expansion): Understanding the exclamation mark (!) in bash. – Stephen Kitt Dec 28 '18 at 9: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. – mosvy Dec 28 '18 at 10:03
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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