Problems with bash - echo with char! (exclamation)

3

I'm trying to pass a password as a parameter so the password uses numbers after the char !

example:

root@LinDom:~# senha="teste!123"
senha="testevim /etc/hosts"
root@LinDom:~#
root@LinDom:~# echo ${senha}
testevim /etc/hosts
root@LinDom:~#

How do I escape the char ! ?

I wanted my result after echo ${senha} to be teste!123

    
asked by anonymous 13.02.2017 / 19:46

2 answers

5

There are 3 types of quotation marks in bash :

  • Double quotes (""): With them you can use strings and variable values. Ex:

nome="Douglas" echo "Olá $nome" Olá Douglas

  • Single quotes (''): With these, the variable will have the exact value inside the quotation marks. Ex:

nome='Douglas' echo "Olá $nome" Olá $nome

  • Inverted quotes (''): When used this means that we will store the output of the command in question. Ex:

home = 'pwd / home / douglas'

In your situation you should use single quotation marks because you want to store the value exactly as it is.

senha='teste!123'
    
13.02.2017 / 20:11
2

@Douglas already said everything, but now only one more explanatory comment of such strange behavior: in bash !... corresponds to the notation referring to the story.

!123 is used to expand to the command numbered 123 in history.

$ history | grep '^ *123'
...
  123  vim /etc/hosts
...

or the strange element that appeared in the password.

Analogously:

  • !c will expand to the last command beginning with the letter c
  • !! the previous command
  • !:2 the second argument of the previous command ...
17.02.2017 / 18:59