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

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

4 Answers 4

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

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

You also need to escape the & character:

$ mysql -umyuser -pone_@\&\!two
1
  • Yes, because the & character is meant for putting a process into background. Commented Jan 21, 2021 at 1:19
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
1
  • interesting answer ;) Commented Jan 4, 2019 at 16:21

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.