Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 [~]# 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!

share|improve this question
you can always use single quotes. to use a single-quote "inside" a single-quoted string, try: ''\' – cas Aug 2 '12 at 10:34
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
management is wise and all-knowing :) – cas Aug 2 '12 at 11:24
up vote 19 down vote accepted

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

share|improve this answer
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
Thanks, this is the way. – dotancohen Aug 2 '12 at 10:36
-bash: !two: command not found

You also need to escape the & character:

$ mysql -umyuser -pone_@\&\!two
share|improve this answer
Thanks! This was the real problem, not the !. – dotancohen Aug 2 '12 at 10:37

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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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