-1

I have the following function in script.py utlizing Python click library:

@click.command()
@click.argument('some-arg', nargs=-1)
def main(some_arg):
    print(some_arg)


main()

The above function gets an unlimited number of command line arguments and prints them - works as expected:

python3 script.py my unlimited CLI args

outputs

('my', 'unlimited', 'CLI', 'args')

However, trying to pass arguments with ( or ) in them results in a bash syntax error:

python3 script.py these are some more arguments (123)

bash: syntax error near unexpected token `('

Is there any way I can pass an argument containing the ( or ) signs without receiving a bash syntax error ?
I know I can wrap it with quotations but this will result in a single string argument being passed. Also, I'm getting my input elsewhere and don't want to tamper with it all that much so wrapping with quotations is kind of a last resort.

8
  • Have you tried quoting it? "(123)" Commented Feb 19, 2018 at 19:45
  • Put the argument in quotation marks. Commented Feb 19, 2018 at 19:46
  • 1
    This has nothing to do with Python. ( is a special character in Bash and needs to be quoted or escaped to be treated literally. Commented Feb 19, 2018 at 19:47
  • 1
    Your command line is first parsed by bash, before the result is passed on to the OS to execute the process, after which Python comes into play. This is not a Python or click problem. Commented Feb 19, 2018 at 19:47
  • Had you typed in the exact bash error message into Google you'd have found plenty of help. I've duplicated this post to one on Stack Overflow that has the exact same issue on the command line, using (number). Commented Feb 19, 2018 at 19:49

2 Answers 2

3

( ) is the syntax for running a command in a subshell in bash.

If you don't want bash to interpret ( ), you need to quote or escape them:

python3 script.py these are some more arguments '(123)'

or

python3 script.py these are some more arguments \(123\)
Sign up to request clarification or add additional context in comments.

Comments

0

The parentheses are reserved characters; just like spaces or wildcard characters, they have to be quoted or escaped if you want to use them literally.

Other reserved characters include ;, &, |, $, ', ", `, etc.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.