44

I have a simple Python script that receives username and password as arguments, but my password contains two exclamation marks. When I call my script like

salafek@dellboy:~/Desktop/$ emailsender.py -u username -p pass!!

a command that I entered earlier replaces the exclamation marks:

salafek@dellboy:~/Desktop/$emailsender.py -u username -p "passemailsender.py -u username -p passwget wget http://www.crobot.com.hr/templog"

I can escape exclamation marks with backslash (\), but my password changes.

Is there solution for this, how can I escape exclamation marks without changing my password?

| improve this question | |
  • The second command doesn't make much sense, what are you trying to do there?? Specially the passemailsender.py -u username -p passwget wget ... part – adamJLev Jul 27 '10 at 18:12
  • 3
    !! is substituted by the shell, it is replaced by the last executed command. This is not specific for python. – Mad Scientist Jul 27 '10 at 18:14
  • 3
    @Infinity - Bash interprets "!!" as the last command you entered. Thus, when he enters the command without escaping his password using single quotes or backslashes, bash inserts his last command where the "!!" was. – Joe Kington Jul 27 '10 at 18:16
68

You should be able to simply wrap things in single quotes in the shell.

$ emailsender.py -u username -p 'pass!!'
| improve this answer | |
19

You need to escape it with \ or quote it with single quotes, otherwise your shell interprets it.

emailsender.py -u username -p pass\!\!

or

emailsender.py -u username -p 'pass!!'
| improve this answer | |
  • 2
    just for the record, escaping works ok if you are not double quoting the argument. Otherwise, the '\' will be written to the file literally. – Bengalaa Jun 13 '15 at 15:49
2

As mentioned by others, this issue isn't specific to Python, but is caused by how you're passing the password parameter to the script.

You'll want to wrap the password string in single quotes to make sure that it's passed to the script exactly as you type it, and isn't interpreted by the shell.

You could do this for the username too, if there's the possibility that it includes an exclamation mark, or other special character.

For example:

emailsender.py -u 'username' -p 'pass!!'
| improve this answer | |
-3

Have you tried

$ emailsender.py -u username -p "pass!!"

EDIT- This won't work. Read comments below

| improve this answer | |

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.