Reputation: 379
I've abc.py
file which accepts argument -p [password]
& -c [command]
.
Now I can run this file as follows :
./abc.py -p 'a!s!d!f' -c 'ifconfig'
a!s!d!f
is my password. As password contains !
character, so I have to send it as argument in ' '
. I tried to send it in " "
but didn't work.
Now I want to run this code as follows :
./abc.py -p 'a!s!d!f' -c './abc.py -p 'a!s!d!f' -c 'ifconfig''
I'm giving ./abc.py -p 'a!s!d!f' -c 'ifconfig'
as a argument to abc.py
The problem is, I'm unable to send '
characher as an argument to abc.py
I need this '
character to be sent as input.
I tried using \
escape character as:
./abc.py -p 'a!s!d!f' -c './abc.py -p \'a!s!d!f\' -c \'ifconfig\''
But not working. How do I do this? Any help would be greatly appreciated.
Upvotes: 2
Views: 5973
Reputation: 26184
You need to quote both '
and !
:
./abc.py -p 'a!s!d!f' -c "./abc.py -p 'a!s!d!f' -c 'ifconfig'"
$ cat p.py
import sys
print sys.argv
In Korn shell:
$ python p.py -p 'a!s!d!f' -c "./abc.py -p 'a!s!d!f' -c 'ifconfig'"
['p.py', '-p', 'a!s!d!f', '-c', "./abc.py -p 'a!s!d!f' -c 'ifconfig'"]
In bash !
is not treated specially only if enclosed in single quotes, so it can be done like this:
$ python p.py -p 'a!s!d!f' -c './abc.py -p '"'"'a!s!d!f'"'"' -c config'
['p.py', '-p', 'a!s!d!f', '-c', "./abc.py -p 'a!s!d!f' -c config"]
Notice that the result is different then when you quote the whole string with double quotes:
$ python p.py -c "./abcy.py -p 'a\!s\!d\!f' -c 'ifconfig'"
['p.py', '-c', "./abcy.py -p 'a\\!s\\!d\\!f' -c 'ifconfig'"]
Upvotes: 2
Reputation: 5995
In Bash (which follows the POSIX shell standard), single quotes preserve every character literally, which means there is no way to escape contents within single quotes. Your choices are:
Concatenate differently-quoted strings by placing them next to each other:
./abc.py -c "./abc.py -p '"'a!s!d!f'"' -c 'ifconfig'"
Use double-quotes and escape the !
characters:
./abc.py -c "./abcy.py -p 'a\!s\!d\!f' -c 'ifconfig'"
Upvotes: 1