Bash

How do I use su to execute the rest of the bash script as that user

27 September 2026 · 4 min read

How do I use su to execute the rest of the bash script as that user

In the complex world of Linux system administration and shell scripting, managing user permissions and executing commands under different security contexts is a fundamental skill. Developers and system administrators often face situations where a script initiated by one user needs to perform specific tasks as another user, perhaps to access protected resources or maintain least privilege. Understanding how do I use su to execute the rest of the bash script as that user is crucial for automating complex administrative workflows securely and efficiently. This guide delves into the practical methods and best practices for achieving this, ensuring your scripts operate smoothly while adhering to robust security principles.

Understanding su and User Contexts in Scripting

The su command, short for “substitute user,” is a powerful utility designed to run commands with the privileges of another user account. Unlike sudo, which typically grants elevated privileges to specific commands based on a configuration file, su allows you to completely switch to another user’s identity, requiring that user’s password (unless switching to root from an already privileged user). This distinction is vital when considering its use within a script, as it directly impacts the script’s environment and security context.

When you use su, it’s not just about changing the user ID; it’s about potentially changing the entire environment. This includes the PATH variable, which dictates where the shell looks for executable commands, as well as home directory, environment variables like HOME, USER, and even the current working directory. Failing to account for these changes can lead to unexpected errors or script failures. For instance, a script might try to execute a command that exists in the original user’s PATH but not in the substituted user’s PATH, resulting in a “command not found” error. Proper management of Linux environment variables becomes paramount.

A common scenario where su is invaluable involves tasks like managing application services. Imagine a scenario where your main deployment script runs as a deployment user, but needs to gracefully restart a web server service that runs under a dedicated www-data or nginx user. Directly executing the restart command as the deployment user might fail due to insufficient permissions. Using su allows the script to temporarily adopt the necessary identity to perform the service restart, then continue its operations as the original user, embodying the principle of privilege escalation only when absolutely required.

The Core Methods: su -c and Here-Documents for Script Execution

When you need to how do I use su to execute the rest of the bash script as that user, there are two primary approaches: using su -c for single commands or employing here-documents for multi-line execution. Each method has its specific use cases and considerations for shell scripting.

Executing Single Commands with su -c

The simplest way to run a command as another user is with the su -c "command" syntax. The -c option tells su to execute the specified command string. For example, if you want to list the contents of a directory only accessible by a specific user, you might use: su -l otheruser -c "ls -la /opt/appdata". The -l option ensures a login shell is simulated, providing a clean environment for the target user. This is effective for isolated commands but quickly becomes unwieldy for more complex, multi-line logic.

Executing Multi-Line Scripts with Here-Documents

For executing a block of commands or an entire script section as another user, here-documents provide an elegant and powerful solution. This technique allows you to embed multiple lines of code directly within your main script, which are then passed as standard input to the su command. To ensure the commands within the here-document are interpreted by the target user’s shell and not the original shell, it’s crucial to quote the terminating marker.

To execute the rest of a bash script Question & Answer :

I’ve written a script that takes, as an argument, a string that is a concatenation of a username and a project. The script is supposed to switch (su) to the username, cd to a specific directory based upon the project string.

I basically want to do:

su $USERNAME; cd /home/$USERNAME/$PROJECT; svn update; 

The problem is that once I do an su… it just waits there. Which makes sense since the flow of execution has passed to switching to the user. Once I exit, then the rest of the things execute but it doesn’t work as desired.

I prepended su to the svn command but the command failed (i.e. it didn’t update svn in the directory desired).

How do I write a script that allows the user to switch user and invoke svn (among other things)?

Much simpler: use sudo to run a shell and use a heredoc to feed it commands.

#!/usr/bin/env bash whoami sudo -i -u someuser bash << EOF echo "In" whoami EOF echo "Out" whoami 

(answer originally on SuperUser)