1

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?

7
  • put it inside backticks? (just a guess im on windows atm) Apr 9, 2013 at 17:29
  • @Joran tried that, exactly the same result.
    – ACarter
    Apr 9, 2013 at 17:30
  • 1
    What is python aliased to? It looks like it might be an empty string...or perhaps echo. What happens if you write $(\python ...)? Do you really want to execute the output of the Python script? Apr 9, 2013 at 17:32
  • 1
    Is that last comma intentional and in the correct place?
    – jpm
    Apr 9, 2013 at 17:32
  • 1
    @ACarter Nope. Arguments are just space delimited: -c command Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).
    – jpm
    Apr 9, 2013 at 17:37

2 Answers 2

6

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.

0
3

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.

1
  • Also, unless the python script is printing a command, this shouldn't be on its own line. Otherwise, it will try to interpret the result as a command. Either do the command w/o command substitution (so it just prints out) or use command substitution to assign the result to a variable.
    – jpm
    Apr 9, 2013 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.