Polite Bash Commands

Making my bash environment a little more polite.


This content originally appeared on Thoughts From Eric and was authored by Eric Meyer

For years, I’ve had a bash alias that re-runs the previous command via sudo.  This is useful in situations where I try to do a thing that requires root access, and I’m not root (because I am never root).  Rather than have to retype the whole thing with a sudo on the front, I just type please and it does that for me.  It looked like this in my .bashrc file:

alias please='sudo "$BASH" -c "$(history -p !!)"'

But then, the other day, I saw Kat Maddox’s tweet about how she aliases please straight to sudo, so to do things as root, she types please apt update, which is equivalent to sudo apt update.  Which is pretty great, and I want to do that!  Only, I already have that word aliased.

What to do?  A bash function!  After commenting out my old alias, here’s what I added to .bash_profile:

please() {
	if [ "$1" ]; then
		sudo $@
	else
		sudo "$BASH" -c "$(history -p !!)"
	fi
}

That way, if I remember to type please apachectl restart, as in Kat’s setup, it will ask for the root password and then execute the command as root; if I forget my manners and simply type apachectl restart, then when I’m told I don’t have privileges to do that, I just type please and the old behavior happens.  Best of both worlds!


This content originally appeared on Thoughts From Eric and was authored by Eric Meyer


Print Share Comment Cite Upload Translate Updates
APA

Eric Meyer | Sciencx (2020-09-29T18:44:54+00:00) Polite Bash Commands. Retrieved from https://www.scien.cx/2020/09/29/polite-bash-commands/

MLA
" » Polite Bash Commands." Eric Meyer | Sciencx - Tuesday September 29, 2020, https://www.scien.cx/2020/09/29/polite-bash-commands/
HARVARD
Eric Meyer | Sciencx Tuesday September 29, 2020 » Polite Bash Commands., viewed ,<https://www.scien.cx/2020/09/29/polite-bash-commands/>
VANCOUVER
Eric Meyer | Sciencx - » Polite Bash Commands. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/09/29/polite-bash-commands/
CHICAGO
" » Polite Bash Commands." Eric Meyer | Sciencx - Accessed . https://www.scien.cx/2020/09/29/polite-bash-commands/
IEEE
" » Polite Bash Commands." Eric Meyer | Sciencx [Online]. Available: https://www.scien.cx/2020/09/29/polite-bash-commands/. [Accessed: ]
rf:citation
» Polite Bash Commands | Eric Meyer | Sciencx | https://www.scien.cx/2020/09/29/polite-bash-commands/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.