4

Say I have these two in my .zshrc:

alias sudo='nocorrect sudo'
alias boot27='grub-reboot 4 && reboot'

boot27 gives boot27: command not found. If I change the alias to 'nocorrect sudo ' or just 'sudo ', it works, but then other things like mkdir give nocorrect: command not found.

1 Answer 1

6

Just to clarify, you run sudo boot27 and get boot27: command not found because since sudo is an alias, bash stops scanning for aliases and does not recognize boot27 as one. As discussed here, one way of dealing with this is adding a space at the end of the alias definition.

Adding the space allows bash to recognize boot27 but the problem now is that your root account is not set to use zsh so sudo starts a bash shell instead. nocorrect is a zsh thing, bash has no idea what it is so it complains.

The simplest way to fix this would be to set root's shell to /bin/zsh:

sudo chsh

Then set your alias with the space:

alias sudo='nocorrect sudo '

Your boot27 alias has another problem though. I assume you run it as sudo boot27, the shell will read the alias and expand that to:

sudo grub-reboot 4 && reboot

So, it will run grub-reboot as root but it will run reboot as a normal user. What you want is either to change your alias to

alias boot27='grub-reboot 4 && sudo reboot'

and run it as sudo boot27, or to change your alias to

alias boot27='sudo grub-reboot 4 && sudo reboot'

and run it without sudo, just boot27.

4
  • Thanks, that helped. I have privileges to reboot as normal user, so I think I'll just leave it as is.
    – Mike Roll
    Jun 9, 2013 at 11:15
  • @terdon I've got a problem with this approach. I've detailed it in a question here, but basically this approach doesn't seem to work for me (while alias sudo='sudo ' works, as does alias sudo='nocorrect sudo'). Have you got any suggestions?
    – Mike H-R
    May 3, 2014 at 13:53
  • @MikeH-R I posted an answer to your question. The issue is already addressed in my answer here, you need to change the sudo shell to zsh.
    – terdon
    May 3, 2014 at 14:03
  • Is there any reason for concern about aliasing non-sudo commands to sudo-prefixed ones? There is sudo -k (alone to invalidate cached credentials, or prefixing a command prompt for credentials on each invocation). I have update aliased to sudo apt-get update, etc. On an unrelated note, I also have 'f***ing' aliased to sudo.
    – John P
    Feb 1, 2017 at 19:58

You must log in to answer this question.

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