Reputation: 5697
I have this command as part of a bash script
$(python -c "import urllib, sys; print urllib.unquote(sys.argv[0])", "h%23g")
But when I run it, I get this:
-bash: -c: command not found
As though bash has missed reading the python
, and is thinking -c
is the name of the command. Exactly the same happens when using backticks.
How can I make bash recognise the python
?
Upvotes: 1
Views: 401
Reputation: 208465
I think you want something like the following:
$(python -c "import urllib, sys; print urllib.unquote(sys.argv[1])" "h%23g")
This will result in h#g
, if this is all you have on a line then it will also attempt to run a command called h#g
, so I'm assuming you are actually using this as a part of a larger command.
The issue with your version is that sys.argv[0]
is the -c
from the command, and urllib.unquote('-c')
will just return '-c'
.
From the documentation on sys.argv
:
If the command was executed using the
-c
command line option to the interpreter,argv[0]
is set to the string'-c'
.
Combining that with info from the man page (emphasis mine):
-c command
Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).
So, when you use -c
, sys.argv[0]
will be '-c'
, the argument provided to -c
is the script so it will not be included in sys.argv
, and any additional arguments are added to sys.argv
starting at index 1.
Upvotes: 3
Reputation: 476
the Python command is returning the string "-c" from your $(...) structure, which bash then tries to execute.
for example
python -c "import urllib, sys; print urllib.unquote(sys.argv[0])"
prints "-c", so you are essentially asking bash to interpret $(-c), for which the error is valid.
Upvotes: 6