Problems with bash - echo with char ! (exclamation)

Asked

Viewed 259 times

3

I’m trying to pass a password as parameter the password uses numbers after 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 make an escape from char ! ?

I wish my result after the echo ${senha} were teste!123

  • 2

    See if this is it http://superuser.com/a/301330/162728

  • That’s right @Guilhermenascimento vlw

  • password='test! 123' does not work?

  • 1

    @Denisrudneidesouza tested the way you typed there in the example the result

  • 1

    @Denisrudneidesouza, sorry I had not noticed that you made reference with the simple quotes, it worked yes, thank you!

  • @Sneepsninja of good, happens

Show 1 more comment

2 answers

5


There are 3 types of quotation marks on 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 quotes. Ex:

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

  • Inverted quotation marks (``): When these are used, this means that we will store the output of the given command. Ex:

home=`pwd /home/Douglas`

In your situation you should use Simple Quotes because you want to store the value exactly as it is.

senha='teste!123'
  • Boy né that with simple quotes worked also...thank you

2

The @Douglas already said everything, but by the way only one more explanatory comment of such strange behavior: in bash !... corresponds to the notation for the history.

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

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

or is the such strange element that appeared in the password.

Analogously:

  • !c will expand to the last command started by the letter c
  • !! the previous command
  • !:2 the second argument of the previous command...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.