17

I am trying to execute

sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application (1995)

but I get this error:

bash: syntax error near unexpected token `('

However,

sudo -su db2inst1 id

gives me correct output. So it must be something about the ()

If I try

sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application \(1995\)

I get

/bin/bash: -c: line 0: syntax error near unexpected token (' \ /bin/bash: -c: line 0: /opt/ibm/db2/V9.7/bin/db2 force application (1995)'

Running /opt/ibm/db2/V9.7/bin/db2 force application (1995) as db2inst1 user gives me the same error, but running

/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"

works fine


The right syntax is

sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"'

2 Answers 2

20

NOTE: While this answer seems to have been correct at the time [sudo was changed later that same year to add extra escaping around characters in the arguments with -i and -s], it is not correct for modern versions of sudo, which escape all special characters when constructing the command line to be passed to $SHELL -c. Always be careful and make sure you know what passing a command to your particular version of sudo will do, and consider carefully whether the -s option is really needed for your command and/or, if it would, if you'd be better served with sudo sh -c.


Since you've got both the shell that you're typing into and the shell that sudo -s runs, you need to quote or escape twice. Any of the following three would have worked with this now-ancient version of sudo:

sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"'
sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 force\ application\ \(1995\)'
sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force\\ application\\ \\\(1995\\\)

Out of curiosity, why do you need -s? Can't you just do the following?

sudo -u db2inst1 /opt/ibm/db2/V9.7/bin/db2 'force application (1995)'
sudo -u db2inst1 /opt/ibm/db2/V9.7/bin/db2 force\ application\ \(1995\)
Sign up to request clarification or add additional context in comments.

1 Comment

the first suggestion doesn't work, you need to move the single quote after db2inst1. The next 2 works fine. Thank you.
3

Try

sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application \(1995\)

3 Comments

@Radek: Not sure what's happening, but that's definitely not a syntax error... :(
@Radek: Yeah I saw your update, but like I said, that's definitely not supposed to be a syntax error; I don't know what's happening. :(
You might need to do \\(1995\\). One escape for the local shell and one for the remote shell. Or maybe even three parens?

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.