0

I write a script to auto install zsh and set some default config.

function setup_zsh(){
    echo "####################################"
    echo "setup zsh"

    apt-get install -y zsh git curl
    sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
    # later code would not execute because now enter zsh shell

    writelines 'source /etc/profile' ~/.zshrc

    echo "setup zsh theme"
    if [ -s ~/.zshrc ]; then
        if grep -q 'ZSH_THEME' ~/.zshrc ; then
            sed -i 's/^ZSH_THEME=.*/ZSH_THEME="'${ZSH_THEME}'"/' ~/.zshrc
        else
            echo 'ZSH_THEME="'${ZSH_THEME}'"' >> ~/.zshrc
        fi
    fi
}

But I found when sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" finishes, it would enter zsh shell, so later code would not execute. How do I work around this behavior?

1

It's because at the end this last line env zsh causes installer.sh run a zsh sub-shell; You can add and redirect 0>/dev/null to sh -c to fail it to run a sub-shell.

0>/dev/null sh -c "$(curl ....)"

You can check 0>/dev/null behavior when running 0>/dev/null env zsh and enz zsh after installation to see that 0>/dev/null env zsh preventing to switch to zsh shell or any other shells.

This will also cause to fail/prevent when that wants to change your default shell when asking your password in below.

if hash chsh >/dev/null 2>&1; then
      printf "${BLUE}Time to change your default shell to zsh!${NORMAL}\n"
      chsh -s $(grep /zsh$ /etc/shells | tail -1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.