There are already some existing questions asked here about running commands as another user. However, the question and answers focus on a single command instead of a long group of commands.

For example, consider the following script:

#!/bin/bash
set -e

root_command -p param1  # run as root

# these commands must be run as another user
command1 -p 'parameter with "quotes" inline'
command2 -p 'parameter with "quotes" inline'
command3 -p 'parameter with "quotes" inline'

There are a couple of important points to note here:

  • The final three commands must be run as another user using su or sudo. In the example there were three commands, but suppose that there were many more...

  • The commands themselves make use of single and double quotes.

The second point above prevents the use of the following syntax:

su somebody -c "command"

...since the commands themselves contain quotes.

What is the proper way to "group" the commands and run them under another user account?

share|improve this question
1  
Any luck on Unix stackexchange? – icedwater Jul 20 '13 at 3:14
    
See also now stackoverflow.com/questions/37586811/… – tripleee Jun 2 '16 at 8:48
up vote 101 down vote accepted

Try this:

su somebody <<'EOF'
command1 -p 'parameter with "quotes" inline'
command2 -p 'parameter with "quotes" inline'
command3 -p 'parameter with "quotes" inline'
EOF

<< introduces a here-doc. The next token is the delimiter, and everything up to a line beginning with the delimiter is fed as standard input to the command. Putting the delimiter in single quotes prevents variable substitution within the here-doc.

share|improve this answer
    
You, sir, are a genius. I had thought of using HEREDOC but couldn't find the correct syntax to make it work. Thanks! – Nathan Osman Jul 20 '13 at 3:43
2  
If you encounter "su: must be run from a terminal", try sudo su somebody <<'EOF'. – Bohr Apr 20 '14 at 3:01
4  
If you use <<EOF instead of <<'EOF' then variables inside the here-doc will be expanded. – Barmar Sep 18 '14 at 15:42
2  
This will be useful to some: If you need the command to actually run in the user's full environment, as if they had logged in, which may include different paths, for example. Add they hyphen before 'somebody', like so: su - somebody ... – JJC Jun 10 '15 at 15:56
1  
Make sure there is no whitespace before closing EOF (eg when calling from indented 'if' statement). Error is thrown otherwise - see tldp.org/LDP/abs/html/here-docs.html – Petr Újezdský Jan 15 '16 at 12:51

I'm not that great with bash-foo so there is a bound to be a more elegant way, but I've approached this problem in the past by using multiple scripts and a "driver"

E.g.

Driver

#!/bin/bash
set -e

su root script1
su somebody script2

Script1

#!/bin/bash
set -e

root_command -p param1  # run as root

Script2

#!/bin/bash
set -e

# these commands must be run as another user
command1 -p 'parameter with "quotes" inline'
command2 -p 'parameter with "quotes" inline'
command3 -p 'parameter with "quotes" inline'
share|improve this answer
#!/usr/bin/env bash

TOKEN_USER_X=TOKEN_USER_X
USER_X=peter # other user!

SCRIPT_PATH=$(readlink -f "$BASH_SOURCE")

if [[ "$@" != "$TOKEN_USER_X" ]]; then

    ###### RUN THIS PART AS the user who started the script

    echo "This script is $SCRIPT_PATH"

    echo -n "Current user: "
    echo $USER

    read -p "insert: "
    echo "got $REPLY"

    su - $USER_X -c "$SCRIPT_PATH $TOKEN_USER_X" # execute code below after else (marked #TOKEN_USER_X)

else
    #TOKEN_USER_X -- come here only if script received one parameter TOKEN_USER_X

    ###### RUN THIS PART AS USER peter

    echo
    echo "Now this script is $SCRIPT_PATH"

    echo -n "Current user: "
    echo $USER

    read -p "insert: "
    echo "got $REPLY"

    exit 0
fi

echo
echo "Back to initial user..."
echo -n "Current user: "
echo $USER
share|improve this answer
1  
Could you please explain what's going on in your script? Further: I think you didn't pay attention to the requirements of the TO regarding the quotes (point #2). – try-catch-finally Jun 27 '14 at 22:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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