The sudo command used to execute a command as another user typically as a root user. This quick tutorial shows you how to use sudo command to run multiple commands via a Linux or Unix shell.
sudo syntax to run multiple commands
The syntax is:
sudo sh -c 'command1 && command2'
sudo -- sh -c 'command1 && command2'
sudo -u userNameHere -- sh -c 'command1; command2'
sudo -- sh -c 'command1; command2'
sudo -- bash -c 'command1; command2'
sudo -i -- 'command1; command2; command3'
sudo -i -- sh -c 'command1 && command2 && command3'
sudo -u oracle -s -- "cmd1; cmd2; cmd3"
Examples
Run date and who am i command:
$ sudo -- sh -c 'date; who am i'
Sample outputs:
[sudo] password for vivek: Tue May 2 19:52:08 IST 2017 vivek pts/0 2017-05-02 18:44 (10.8.0.2)
You can run command as mysql user:
$ sudo -u mysql -- sh -c "/home/mysql/backup.sh; /home/mysql/mirror.py"
Update your server in a single go using sudo with the help of apt-get command:
$ sudo -- sh -c 'apt-get update && sudo apt-get -y upgrade'
Animated gif 01: sudo command in action
Understanding sudo command options
To run multiple commands sudo we used the following options:
- -- : A -- signals the end of options and disables further option processing for sudo command.
- sh -c : Run sh shell with given commands
- bash -c : Same as above.
- 'apt-get update && sudo apt-get -y upgrade' : First update repo and apply upgrades if update was successful.
- -i : Execute the shell specified by the target user’s password database entry as a login shell. This means that login-specific resource files such as .profile, .bash_profile or .login will be read by the shell.
- -s : Run the shell specified by the SHELL environment variable if it is set or the shell specified by the invoking user’s password database entry.
A note about using sudo command in a shell script
Here is a sample shell script that shows how to use or run multiple commands with sudo:
#!/bin/bash echo "Running commands as a root user..." sudo -- -sh -c <<EOF apt-get update apt-get -y upgrade apt-get -y install nginx apt-get -y remove nano apt-get clean echo "All done." EOF
A note about using sudo with bash shell aliases
The syntax is as follows for shell aliases:
alias foo="sudo -- sh -c 'cmd1 && cmd2'" alias bar='sudo -- sh -c "cmd1 && cmd2"'
For example add the following to ~/.bashrc or ~/.bash_aliases file:
# update debian/ubuntu box using apt # command line when type update alias update='sudo -- sh -c "apt update && apt upgrade"'
See how to create a permanent Bash alias and 30 handy bash shell aliases for more info.
Conclusion
You learned how to run multiple commands using sudo command under a Linux or Unix-like systems. For more info see sudo man page by typing the following command or visit this page here:
man sudo
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 1 comment... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
A simpler solution is su root, then run your commands then ^D to get out. Contrary to what some folks say there is nothing intrinsically wrong with conducting commands as root, you just need to take great care, and think before you push key. If your very concerned, put your commands in a shell script and fire that off as root. The more you do stuff the more confident you become.
best of luck, OBB