Shell 升级后.bashrc 中找不到 shopt 命令

我已经更新我的 shell 到 ZSH。当我 source ~/.bashrc。我得到这个错误

你的医生说错了。 当我执行这个命令时

echo "export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules" >> ~/.bashrc && source ~/.bashrc

/home/amerrnath/. bashrc: 17: command not found: shopt/home/amerrnath/. bashrc: 17: command not found: shopt /home/amerrnath/. bashrc: 25: command not found: shopt/home/amerrnath/. bashrc: 25: command not found: shopt /home/amerrnath/. bashrc: 109: command not found: shopt /usr/share/bash- 完成/bash _ 补全: 35: 解析“]]”附近的错误

请帮我解决这个问题

146520 次浏览

Your bashrc file was written for bash. zsh is not bash.

I'm surprised zsh is trying to load your .bashrc at all.

If it isn't and you are sourcing it manually (from .profile or similar). Stop doing that.

Then you get to write an appropriate zsh init file instead.

If you want to use zsh then you need to use zsh and not bash.

shopt is a bash-ism.

[[ is a bash-ism.

shopt is not a command, but a shell built-in. bash knows what to do with it because it's a bash built-in , but zsh has no idea what it is. You'll want to look into setopt which is a zsh built-in, and put those values into a new .zshrc script.

zsh uses env profile ~/.zshrc, not ~/.bashrc.

so you need to append your env settings to .zshrc file and then

source ~/.zshrc

It must work.

rbenv github link

To place anything in ~/.bashrc:

Switch to bash:

exec bash

Then

source ~/.bashrc

Switching to bash will not effect on new terminal window. But if you want to switch current window to zsh.

Switch to zsh:

exec zsh

reference

For some reason after the upgrade from 16.04 to 17.10 and to 18.04, the symlink /bin/sh was set back to dash not bash. Updating this link:

sudo cd /bin && ln -sf bash sh

solved this problem for me

Make an alias of shopt and call it through zsh

A quick solution is described here: https://github.com/larz258/Zshopt

sudo vi /usr/bin/shopt

Inside the shopt

#!/bin/bash
args='';
for item in $@
do
args="$args $item";
done
shopt $args;

make it executable

sudo chmod +x /usr/bin/shopt

Create an alias in your .zshrc

echo "alias shopt='/usr/bin/shopt'" >> ~/.zshrc

shopt is not a command, but a shell built-in. You can find out this by running the following command in bash:

type shopt

output would be:

shopt is a shell builtin

solution:

step1:

echo "#! /bin/bash\n\nshopt \$*\n" > /usr/local/bin/shopt

then you will get /usr/local/bin/shopt:

#! /bin/bash


shopt $*

step2:

chmod +x /usr/local/bin/shopt

step3:

ln -s /usr/local/bin/shopt /usr/bin/shopt

step4:

echo "alias shopt='/usr/bin/shopt'" >> ~/.zshrc