How to Run a Shell Script as Superuser

If you've ever tried to run a shell script as superuser in Linux by using the syntax sudo ./example-shell-script, you'll know that doesn't really work – the commands within the script are executed as you, not the superuser, so permissions issues can easily arise. Instead, if you want to run the script as superuser, this is how to do it:

  1. Create your script
    vi example-shell-script
  2. Change the permissions
    chmod 755 example-shell-script
  3. Switch into superuser mode
    sudo su -
  4. Run the script
    ./example-shell-script
  5. Log out of superuser mode
    exit

That's it. You're done. Pretty easy, right?


Comments

Loading…

This post was published on October 4th, 2009 by Robert James Reese in the following categories: Linux and VI Editor. Before using any of the code or other content in this post, you must read and agree to our terms of use.