7

I have some custom functions and aliases defined in my .bashrc file. Sometimes I need to execute whem as root. I then do something like:

$ sudo custom_cmd 80                                                                                                                                   
sudo: custom_cmd: command not found

How can I execute such commands with root privileges?

EDIT: Of course to achieve that I could simply source my .bashrc as root but I want to know if there is some quick (in terms of typing needed) way to do this. I also would like to avoid any customization if that is possible (such as sourcing my .bashrc automatically).

2 Answers 2

9

Run it through your shell:

sudo bash -c 'source /home/reachus/.bashrc; custom_cmd 80'

Alternatively, write a script which sources .bashrc for you, say /usr/local/bin/my:

#! /bin/bash
source /home/reachus/.bashrc
"$@"

Then do:

sudo my custom_cmd 80
4
  • Yes, I can obviously source my .bashrc file but I am looking for more quicker solution. I'll edit my question.
    – syntagma
    Jan 27, 2015 at 20:01
  • @REACHUS write a script which does the same thing: source .bashrc; "$0" "$@".
    – muru
    Jan 27, 2015 at 20:02
  • You're right, that should be enough.
    – syntagma
    Jan 27, 2015 at 20:06
  • @REACHUS argh, I mean: simply "$@", since $0 would be the script name.
    – muru
    Jan 27, 2015 at 20:07
1

Just had the same problem.

You can also

  cd /etc/profile.d
  sudo gedit ./newfile.sh (you write commands here, can be nano also)
  sudo chmod +x newfile.sh

It should run automatically whenever you login to your account. To test it, instead of restarting, just run "su yourusername" in bash and login and it should work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .