Bashrc – Run Command with Sudo

bashbashrcsudo

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).

Best Answer

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