34

How might one escape the exclamation point in a password:

$ mysql -umyuser -pone_@&!two
-bash: !two: event not found

Trying the obvious backslash did not help:

$ mysql -umyuser -pone_@&\!two
[1] 22242
-bash: !two: command not found
name@domain.com [~]# ERROR 1045 (28000): Access denied for user 'myuser'@'localhost' (using password: YES)

All my google searches suggest that the backslash would help, but it does not. There is no way to use quotes as suggested in this question. The line will be used in a .bashrc alias. Don't worry, the usernames and passwords shown here are examples only and not used in production!

dotancohen
  • 2,590
  • 2
  • 25
  • 39
  • you can always use single quotes. to use a single-quote "inside" a single-quoted string, try: `''\'` – cas Aug 02 '12 at 10:34
  • 6
    BTW, putting the password on the command line is a potential security risk on a multi-user system. It is trivially easy to examine the command-line args of any running process. Use a .my.cnf file instead (remember to `chmod 600` it). – cas Aug 02 '12 at 10:37
  • 1
    oops. that's `'\''`, not `''\'` – cas Aug 02 '12 at 10:40
  • Thanks, Craig. Actually, this is more secure than the alternative: emailing everyone a copy of the password which will then be store who-knows-where. I personally would prefer that each dev has his own `/home/user` and mysql user, but I'm not the decision maker in that regard. – dotancohen Aug 02 '12 at 11:01
  • 2
    management is wise and all-knowing :) – cas Aug 02 '12 at 11:24

4 Answers4

46

Use single quotes around the password like this: -p'one_@&!two'

To put it in an alias, you'd do something like:

alias runmysql='mysql -umyuser -p'\''one_@&!two'\'''

cas
  • 6,783
  • 32
  • 35
5
-bash: !two: command not found

You also need to escape the & character:

$ mysql -umyuser -pone_@\&\!two
quanta
  • 51,413
  • 19
  • 159
  • 217
5

If you never use the ! history features, it might be more convenient to simply disable them (with set +H in your bashrc).

3

You can store the password in a bash variable:

$ pass='one_@&!two'

Then substitute the variable in the command:

$ mysql -umyuser -p$pass