未设置 BASH_VERSION 且交互式 shell 中未获取 .bashrc

未设置 BASH_VERSION 且交互式 shell 中未获取 .bashrc

我通过执行创建了一个新用户

sudo useradd -m harry

当我尝试用“su - harry”替换用户时,我看到的所有内容都是“$”(美元)符号,自动完成功能不起作用,并且 shell 内置命令(例如 source)不可用。

我检查了 .bashrc 和 .profile,发现 .bashrc 没有被获取,因为 $BASH_VERSION 还没有设置。

.profile 里面的代码如下:

# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi
fi

该命令echo $BASH_VERSION不返回任何内容。

$ echo $BASH_VERSION

$ 

Shell 内置命令(源 .bashrc、. .bashrc)也不起作用。

SHELL 变量值是 '/bin/sh',它是 dash 的别名。我将其更改为 /bin/bash(export SHELL=/bin/bash),但没有任何效果。

当我在 .profile 中对“. .bashrc”进行硬编码时(当然,只是为了检查它是否有效)我收到了几个错误:

admin@host:~$ sudo su - harry
-su: 13: /home/harry/.bashrc: shopt: not found
-su: 21: /home/harry/.bashrc: shopt: not found
-su: 105: /home/harry/.bashrc: shopt: not found
-su: 28: /etc/bash_completion: [[: not found
-su: 34: /etc/bash_completion: [[: not found
-su: 51: /etc/bash_completion: Bad substitution
\[\e]0;\u@\h: \w\a\]\u@\h:\w$ 

有什么想法可以解决它吗?

提前谢谢你。 jepetko

答案1

该用户的 shell 是/bin/sh。这是一个较小的 shell,它比 bash 占用更少的资源,但提供的编程功能较少,而且几乎没有交互功能。

运行sudo chsh -s /bin/bash harry以将用户的登录 shell 更改为 bash。这会更改用户条目/etc/passwd(除非您知道自己在做什么,否则不要直接编辑该文件)。

设置SHELL环境变量会告诉应用程序运行不同的 shell,但对当前正在运行的 shell 没有影响。

答案2

如果您希望默认创建具有 bash shell 的用户,请使用以下命令:

useradd -m -s /bin/bash <userName>

替换<userName>为您要创建的用户的名称。

相关内容