I'm creating a shell script that needs to update the sudoers file. So basically I want to be able to append ":/usr/local/bin/" to the following text in /etc/sudoers:

secure_path = /sbin:/bin:/usr/sbin:/usr/bin

I need to use sudo in the command in order to gain the permissions to modify. Any idea how I can accomplish that?


So many ways!

The old standby is sed: sed 's/\(secure_path.*\)/\1:\/usr\/local\/bin/' /etc/sudoers

Scripts in vim can do advanced things as well. Any process you would type into vim can be saved to a vim script and played back.

The newest tool that's really handy for this is Augueas. It's often used with Puppet is structured around editing files in a structured manner. Thus, it's aware of context and hierarchies within a text file.

  • Thanks for the answer. However, I get the following when I try that command: sed: -e expression #1, char 22: unknown option to `s' – VinnyD Apr 22 '12 at 22:49
  • @VinnyD Oops... I fixed up the escaped characters and quoting and tested it. Should work now. – Jeff Ferland Apr 22 '12 at 23:26

best thing to use is : sed - stream editor for filtering and transforming text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.