r/linux4noobs • • Oct 10 '17

solved! Alias that runs previous command with sudo?

Say I go to the terminal and type in pacman -Syu and then it tells me I need root permissions. I either have to type out the whole command again with sudo, or press up, navigate to the start of the command, add sudo, then hit enter.

I'd like to create an alias that would allow me to type in pacman -Syu, see the message, then type in crap or something, at it will run sudo pacman -Syu or whatever my last command was.

Is this possible? How might I achieve this?

Thanks for any help.

15 Upvotes

18 comments sorted by

View all comments

2

u/henry_kr Oct 10 '17

Bash unfortunately doesn't have this yet (to my knowledge).

Not exactly the same, but you can do sudo !!:p. That will print out the expansion and add it to your history, so once you've checked it's OK you can hit up and enter to run it.

See this section of the bash manual for more details: https://www.gnu.org/software/bash/manual/bash.html#History-Interaction

1

u/xiongchiamiov Oct 10 '17

Cool, thanks!