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!

  • you can always use single quotes. to use a single-quote "inside" a single-quoted string, try: ''\'
    – cas
    Aug 2 '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 2 '12 at 10:37
  • oops. that's '\'', not ''\'
    – cas
    Aug 2 '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 2 '12 at 11:01
  • 2
    management is wise and all-knowing :)
    – cas
    Aug 2 '12 at 11:24

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'\'''

  • 1
    no space between the -p and password, but yes, this should work
    – mulaz
    Aug 2 '12 at 10:33
  • already edited out the space :)
    – cas
    Aug 2 '12 at 10:33
-bash: !two: command not found

You also need to escape the & character:

$ mysql -umyuser -pone_@\&\!two
  • Yes, because the & character is meant for putting a process into background. Jan 21 at 1:19

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


You can store the password in a bash variable:

$ pass='one_@&!two'

Then substitute the variable in the command:

$ mysql -umyuser -p$pass
  • interesting answer ;)
    – chapskev
    Jan 4 '19 at 16:21

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.