0

I have a problem with the following command

sudo -u ${USER} -H sh -c "bash ${START_SCRIPT}"

I can exclude errors with the START_SCRIPT, since

bash ${START_SCRIPT}

works excellent.

I think there must be a problem with the sudo-syntax, but I can't find the answer. Everywhere (e.g. this link, answer by Kimvais or the sudo manpages) suggest the formatation I used above.

USER=some_user START_SCRIPT=/some/long/path/start.sh

I can only guess that it has to do with the long path or that I miss any arguments for the sudo-command.

In this thread I read:

sudo -H -u otheruser bash -c 'echo "I am $USER, with uid $UID"'

That works perfectly, but neither

sudo -H -u otheruser bash -c '${START_SCRIPT}'

nor

sudo -H -u otheruser -c 'bash "${START_SCRIPT}"'

works.

Can anyone help me please or at least give a hint?

Community
  • 1
  • 1
Damian
  • 19
  • 1
  • 5
  • Why would you run `sh` just to run `bash`? – tripleee Sep 14 '15 at 11:48
  • You cannot have spaces around the equals sign in the assignments. If you have those errors in your actual script, both `USER` and `START_SCRIPT` will be empty variables (and you should get error messages saying `USER: command not found` etc). – tripleee Sep 14 '15 at 11:50
  • Hi tripleee! Thanks for your input. There are no spaces, sorry. I've edited the thread. – Damian Sep 14 '15 at 11:52
  • `sudo -H -u otheruser -c 'bash "${START_SCRIPT}"'` is also a syntax error (`c` is not a valid option for `sudo`) but `sudo -H -u otheruser bash "${START_SCRIPT}"` should work if `START_SCRIPT` really contains the value you expect. – tripleee Sep 14 '15 at 11:52
  • 1
    Also, if `START_SCRIPT` has a proper valid [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) , simply `sudo -H -u otheruser $START_SCRIPT` should work as well (or better). – tripleee Sep 14 '15 at 11:53
  • Single quotes around `'$START_SCRIPT'` will prevent variable substitution, so you will get the literal string inside the single quotes, not the value of the shell variable. – tripleee Sep 14 '15 at 11:55

1 Answers1

0

The single quotes in

sudo -H -u otheruser bash -c '${START_SCRIPT}'

prevent the shell from expanding the $. Simply use double quotes:

sudo -H -u otheruser bash -c "${START_SCRIPT}"
Daniel
  • 42,087
  • 4
  • 55
  • 81