6

So, I would like to keep bash as the default shell for root.

But, I would prefer to use zsh, my shell, when I run sudo commands, and that sudo executes also the .zshrc in /root.

For testing purposes, I have created an alias in my /root/.zshrc and ~/.zshrc :

alias test1='echo "Test OK"'

Now, when I run sudo su enter zsh enter test1 enter

I got the expected result : Test OK

However, all of these commands result in : test1: command not found

sudo test1
sudo su -c 'zsh -c test1'
sudo -E zsh -c 'test1'
sudo ZDOTDIR=/root/.zshrc test1
sudo ZDOTDIR=/root/.zshrc zsh -c 'test1'
sudo ZDOTDIR=/root/.zshrc zsh -c 'source /root/.zshrc && test1'

I found this question on askubuntu, but I think this is more related to this one on Unix & Linux.

However, none of these posts help me.

What I want to achieve in final is to create a simple alias that could replace sudo when I want to use zsh with its /root/.zshrc file, and prefix the command I want to run.

Any hints ?

1 Answer 1

5

I'm not a zsh user, but sudo zsh -c test1 is failing because the shell has not read your .zshrc file. Try this:

sudo zsh -ic test1

That should tell zsh that this is in interactive shell, so source the .zshrc file.

4
  • Thanks, that works. But, how can I write my alias command to prevent quoting my commands ?
    – aklmie
    Aug 14, 2015 at 0:07
  • I don't understand your question. Aug 14, 2015 at 0:08
  • See the last paragraph of my question. I want to write an alias (say sudozsh and use it in the same way of sudo). alias sudozsh='sudo zsh -ic' doesn't work with commands such as tail -f /var/log/syslog. I would like also not having to backslash quotes in commands.
    – aklmie
    Aug 14, 2015 at 0:14
  • You'll have to handle shell control operators like && specially, so use your alias, but quote the command to send: sudozsh 'tail -f', sudozsh 'cd /remote/dir && tar cf - .' | tar xf - Aug 14, 2015 at 1:18

You must log in to answer this question.

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