I need to run sudo -u db2inst1 sh -c "db2 connect to db; db2 set schema Edumate; db2 \"select * from edumate_settings\"" but I don't want to specify absolute path to db2

If I run sudo -u db2inst1 sh -c "id;$PATH;ls /opt/ibm/db2/V9.7/bin/db2"

I get correct $PATH where /opt/ibm/db2/V9.7/bin/ is present

uid=1002(db2inst1) gid=107(db2iadm1) groups=16(dialout),33(video),107(db2iadm1),108(db2fadm1),1001(eduserver)
sh: /sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/usr/games:/usr/lib64/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/opt/firebird/bin:/opt/ibm/db2/V9.7/bin/:/opt/eduserver/bin: No such file or directory

But if I run sudo -u db2inst1 sh -c "db2" I get sh: db2: command not found

If I use the absolute path (I don't want to use absolute path in case it gets changed) I get db2 prompt.

Any suggestion how to call only db2 in this command sudo -u db2inst1 sh -c "db2 connect to db; db2 set schema Edumate; db2 \"select * from edumate_settings\""


In most configurations, sudo replaces the PATH environment variable by a compile-time default or a value specified in /etc/sudoers.

You can do the path lookup before passing the command to sudo.

db2=$(unset -f db2; unalias db2 2>/dev/null; command -v db2)
sudo -u db2inst1 sh -c '"$0" connect to db; "$0" set …' "$db2"

In the common case when you know that the command is not a function nor an alias in your current shell and that the full path doesn't contain any special character:

sudo -u db2inst1 sh -c "$(command -v db2) connect to db; $(command -v db2) set …"

Note the use of double quotes here, so that $(command -v db2) is evaluated in the outer shell. You can feed commands on standard input to save more typing:

sudo -u db2inst1 $(command -v db2) <<\EOF
connect to db
set …
  • Hm, I need to find the path as db2inst1 user not as current user. Current user doesn't have access to this file. – Radek Nov 7 '12 at 0:31
  • Could you explain why $PATH is displayed correctly for sudo -u db2inst1 but running a file that is in the $PATH fails? – Radek Nov 7 '12 at 0:32
  • @Radek Oh, I think I misread the question, your symptoms are not exactly the usual ones. What is the output of sudo -u db2inst1 sh -c "type db2"? Do you have access to the sudoers file, and if so, what are the relevant settings (the applicable Defaults lines and the line that let you run db2 as db2inst1)? – Gilles Nov 7 '12 at 0:38
  • sudo -u db2inst1 sh -c "type db2" gives me sh: line 0: type: db2: not found – Radek Nov 7 '12 at 0:47
  • 1
    @Radek Scratch that, I misread your question the second time. sudo -u db2inst1 sh -c "id;$PATH;ls /opt/ibm/db2/V9.7/bin/db2" expands PATH in the outer shell. You do find db2 in the current user's path, not in db2inst1's path. – Gilles Nov 7 '12 at 0:56
sudo -u db2inst1 -i sh -c "db2 connect to DBNAME"
  • 1
    How does that answer the question? – RalfFriedl Jan 4 at 16:24
  • the only adjustment from the question is the addition of the -i flag, which could be made explicit. – Jeff Schaller Jan 4 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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