Mar 17, 2011

f Comment

How to Escape Command Line Arguments in Unix/Linux Terminal?

Amazon Recently I am trying to use 'mysql' to connect to my MySQL server and I'd like to include the password in the command line. My password is "Crack!t" without quotes. However when I enter the command I get an error. What should I do?

Escaping special characters in the command line in any Unix shell can be confusing and frustrating. For me I am merely trying to connect to a MySQL database specifying the user name and password as the parameters but I don't realize it's that difficult! Here's the complete command for me to connect to my local MySQL server with user name set to 'owner' and database set to 'mffl':

mysql -uowner mffl -pCrack!t
Obviously it doesn't work. The problem is that the bang character (!) is a special character in shells that can be used to re execute a previously entered command. Anyway the output of executing the above command looks like:

$ mysql -uowner mffl -pCrack!t
mysql -uowner mffl -pCracktouch a
mysql Ver 14.12 Distrib 5.0.67, for debian-linux-gnu (x86_64) using readline 5.2
Copyright (C) 2000-2008 MySQL AB
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL license
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
...
...

It sucks doesn't it. Look below to find the solution!

Solution
To solve it, simply use \ to escape it:

mysql -uowner mffl -pCrack\!t
It should work now. If not let me know. Now whenever you need to escape special characters in a command line use a backslash (\) to do it. If you are not familiar with special characters please read A List of Special Characters In Unix Bash Shell!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael