Can I execute multiple lines sudo commands?

There's several approaches to letting multiple commands run with sudo.

The first one suits your use-case best. Others are added for the sake of exploring alternatives; they may not suit your particular need, but may come in handy to you in the future or other users.

Approach #1: Cleanest and simplest

Put everything into a script, for example myscript.sh, make the file executable with chmod 755 myscript.sh, and run that script with sudo myscript.sh

Approach #2: long string of commands

You need something with superuser privileges to run multiple commands. That would be shell, ( any shell for that matter, bash or sh ) . Shells have a -c flag, which tells them to run a command or a set of commands separated by a semicolon ;. Simple example: sudo bash -c 'apt-get update; apt-get upgrade'. In particular, kyodake's answer can be re-done like so:

sudo bash -c 'apt-get update;apt-get install --reinstall aptitude deborphan mate-desktop; aptitude remove '?and(?reverse-depends(unity),?not(?reverse-depends(?exact-name(mate-desktop))))';aptitude remove '?and(?reverse-depends(ubuntu),?not(?reverse-depends(?exact-name(mate-desktop))))';aptitude remove '?and(?reverse-depends(gnome),?not(?reverse-depends(?exact-name(mate-desktop))))';apt-get install --reinstall mate-desktop;deborphan;apt-get --purge remove $(deborphan);deborphan --libdevel;apt-get --purge remove $(deborphan --libdevel);deborphan --find-config;dpkg --purge $(deborphan --find-config);apt-get autoremove;apt-get clean;reboot'

Lengthy ? Yes. This approach works better when you need to run just a few commands with sudo.

Approach #3: playing with sudo timeout

By default, the sudo privilege times out after 15 minutes. So if you are certain all those commands won't take that long, you could run the first command with sudo and will be prompted for password; for all the subsequent commands you won't be prompted, but you still need to type sudo.

Approach #4: using root shell

As kyodake suggested himself in the answer, log-in to a root shell with sudo -i to use the root privilege until you exit. Note, that this is not recommended for security reasons. Unless you are 100% certain you will remember to log out once you are done working and no-one will get into your root shell, then... use this approach at your discretion.


Copy-pasting the script will work, and the commands will be executed one after the other;

However the problem here are the $ / # characters at the start of each line, which would break each command; removing them is mandatory before running the script in any way.

I think the fastest / most direct way to do this would be to process it and to run it at the same time using the following method:

  • Selecting the script, skipping the first line, as it is (i.e. including the # characters at the start)
  • Copying the script using Ctrl+C
  • Hitting Ctrl+Alt+T
  • Typing sed 's/^.//' <<EOF | sudo bash
  • Hitting Enter
  • Pasting the script using Ctrl+Shift+V
  • Hitting Enter
  • Typing EOF
  • Hitting Enter again

Advantages against creating a script on purpose and running it:

  1. It's quicker
  2. There's no need to create any file
  3. There's no need to process the script to remove the $ / # characters

Here's an example using this script:

# whoami
# echo command1
# echo command2
# echo command3
ubuntu@ubuntu ~ % sed 's/^.//' <<EOF | sudo bash
ubuntu@ubuntu ~ pipe heredoc> # whoami
ubuntu@ubuntu ~ pipe heredoc> # echo command1
ubuntu@ubuntu ~ pipe heredoc> # echo command2
ubuntu@ubuntu ~ pipe heredoc> # echo command3
ubuntu@ubuntu ~ pipe heredoc> EOF
root
command1
command2
command3