0

I am trying to write a python file that takes command line input and performs some actions. The input will consist of a-z, [, ], ( and ). I made the following program just to check that I could keep going:

#!/usr/bin/env python
import sys

print str(sys.argv)

I did chmod +x program and tried calling ./program qwerty (abc) [hi] and it returned:

-bash: syntax error near unexpected token `('

Is there any way of changing the program so that it accepted parentheses in arguments?

Note: I have also tried placing square brackets before parentheses and it returns the same error.

3
  • 4
    () has special meaning to shell - it invokes the enclosed command in a subshell. Enclose your strings in double quotes to remove the special meaning - ./program qwerty "(abc)" "[hi]" Commented Aug 17, 2017 at 4:54
  • I'm following a spec and won't be able to change the input, edited question above. Commented Aug 17, 2017 at 4:58
  • 1
    It's failing in the shell before the program runs, so no, you can't change it in the program. Commented Aug 17, 2017 at 5:17

1 Answer 1

2

There's nothing the script can do about shell syntax when invoking the script. The shell parses the command line first. You have to escape or quote characters that have special meaning in the shell (which includes most punctuation characters):

./program qwerty \(abc\) '[hi]'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.