Skip to main content
4 of 5
added 23 characters in body
Rakib Fiha
  • 660
  • 4
  • 10
  • 24

sudo without sudo, implying sudo in script

I made some scripts containing some functions which by design needs sudo permission. I have added those path in .bashrc for Linux and .bash_profile for MacOS so that it can be called from anywhere.

But I do not want the user to type sudo each time they want to call those script functions.

Is there any way I can imply sudo in a way that whenever these functions are called, terminal would assume its being called from root user?

I think I should just add sudo -i at the beginning of the script or maybe each function? Or is there any other alternative way of implying sudo?

An example of dangerous-function script that contains some functions whcih, I am trying to do without sudo

#!/bin/bash
start-one()
{
    ## do dangerous stuff with sudo
    systemctl start dangerous.service
}

start-two()
{
    systemctl start dangerous1.service
}

start-launchwizard()
{
    systemctl start dangerous2.service
}

## Calling functions one by one...
"$@"

I dont want to call them by sudo dangerous-function start-one I just want to call them with dangerous-function start-one but still get the same result as the previous one.

Rakib Fiha
  • 660
  • 4
  • 10
  • 24