Hi ,
I want to run a shell script which is present in another user's id after switching it to that user in the same script. but it is not taking it. I m using the su command for that.

su - user1 -c "./${HOME}/script1.sh"

it just says invalid command. Please help. I need it urgently.

Dani AI

Generated

Short answer: the shell that builds the command string runs before you switch users, so variable expansion and quoting matter — that’s why the ./${HOME}/... approach failed. was right to flag expansion and to suggest using a tool that runs commands as another user. Below are practical ways to run multiple commands as the target user and (optionally) stay in that user’s shell.

Use a login shell or feed commands on stdin (heredoc). Single‑quote the heredoc delimiter so the original shell does not expand variables; the commands are executed by the target account’s shell and see the correct HOME, PATH, etc.

sudo -iu user1 <<'EOF'
cd ~
./script1.sh
./postproc.sh arg
# optionally start an interactive login shell
exec bash --login
EOF

If you want to replace the current process and drop straight into the other user’s interactive shell (script stops there), use exec:

exec su - user1
# this does not return to the invoking script

If you prefer a one‑liner, pass a single quoted command string so expansions are done by the invoked shell:

su - user1 -c 'cd "$HOME" && ./script1.sh && ./cleanup.sh'

Quick troubleshooting and hardening tips: confirm the target script is executable and has a correct shebang; verify the target user’s shell in /etc/passwd (it must not be nologin); use absolute paths or let the target shell expand ~; check ownership and permissions; for unattended automation configure a narrow sudoers rule granting only the needed command(s) rather than a blanket passwordless switch. For the immediate problem, try the heredoc or the quoted -c string so commands run in the target user’s environment.

Recommended Answers

All 2 Replies

Hi ,
I want to run a shell script which is present in another user's id after switching it to that user in the same script. but it is not taking it. I m using the su command for that.

su - user1 -c "./${HOME}/script1.sh"

it just says invalid command. Please help. I need it urgently.

1. $HOME is expanded in the current shell, i.e. before you have changed users.

2. $HOME is not a subdirectory of the current directory; which is what you are saying when you use ./${HOME}/

3. In scripts, use 'sudo -u user1 ...'.

4. Don't put scripts in users' home directories; put them in a directory in your PATH

is it possible if we want to run a number of commands after switching the user and remain in that shell only.

su - newuser -c"command"

allows only one command to run and in that also the home dir is of the old user.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.