别名设置在登录时不起作用

别名设置在登录时不起作用

新安装的 hirsute = 21.04。从软件包中安装,创建我自己的个人帐户,默认的 .bashrc 存在于我的主目录中,并确保也有一个 .bash_aliases。两者都提到 alias ll='ls -alF' 登录后此别名仍然不可用。这是一个错误还是我遗漏了什么?

NAME="Ubuntu"
VERSION="21.04 (Hirsute Hippo)"

karel@schal:~$ pwd ; ls -al .bash*
/home/karel
-rwxr-xr-x 1 karel users   53 Sep 26 06:22 .bash_aliases
-rw------- 1 karel users 9834 Sep 26 06:23 .bash_history
-rw-r--r-- 1 karel users 3771 Aug 31 23:17 .bashrc
karel@schal:~$ cat .bash_aliases
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'
karel@schal:~$ ll
ll: command not found

在一条不太友好的评论后进行了编辑,添加:

karel@wiske:~$ ssh [email protected]
[email protected]'s password: 
Welcome to Ubuntu 21.04 (GNU/Linux 5.11.0-34-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

0 updates can be applied immediately.


The list of available updates is more than a week old.
To check for new updates run: sudo apt update
Last login: Sun Sep 26 09:37:21 2021
karel@schal:~$ alias
karel@schal:~$ /bin/bash
karel@schal:~$ alias
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

并且无论是通过 ssh 登录还是在本地图形环境中或本地纯文本控制台(dev/tty5 和类似控制台)中登录,行为都是相同的

此外,根据要求,摘录自~/.bashrc:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

[根据要求进一步编辑]

karel@schal:~$ ls -al ~/.bash_profile  ~/.bash_login ./.profile
ls: cannot access '/home/karel/.bash_profile': No such file or directory
ls: cannot access '/home/karel/.bash_login': No such file or directory
ls: cannot access './.profile': No such file or directory
karel@schal:~$ ps -p $$ | tail -n1 | awk '{print $NF}'
bash

答案1

通过 ssh 登录时,您正在运行所谓的交互式登录外壳,而不是交互式非登录 shell这就是登录后打开终端时发生的情况。登录 shell 不会读取~/.bashrc,而是读取~/.profile~/.bash_profile并且~/.bash_login。这就是您的别名不存在的原因。有关此问题的详细信息以及各种 shell 类型的初始化文件之间的差异,请参阅为什么 /etc/profile.d/ 中的脚本被忽略(系统范围的 bash 别名)?。这也是为什么您在运行时会获得别名的原因,/bin/bash因为它会启动非登录 shell 并读取~/.bashrc

也就是说,Ubuntu 的默认~/.profile文件包含以下几行:

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

所以它实际上也应该读取你的~/.bashrc。如果没有发生这种情况,我怀疑是以下原因之一:

  1. 您(或某人)创建了一个~/.bash_profile文件。这将导致该~/.profile文件被忽略。如man bash(重点是我的)中所述:

    当 bash 作为交互式登录 shell 或使用 --login 选项作为非交互式 shell 调用时,它首先从文件 /etc/profile 读取并执行命令(如果该文件存在)。读取该文件后,它按顺序查找 ~/.bash_profile、~/.bash_login 和 ~/.profile,然后从第一个存在且可读的文件中读取并执行命令. 启动 shell 时可以使用 --noprofile 选项来抑制此行为。

    因此,如果~/.bash_profile~/.bash_login存在,则 中的任何内容~/.profile都会被忽略。

  2. 您(或其他人)创建了自己的~/.profile没有来源的版本~/.bashrc

  3. 您实际上并没有运行bash。您可以通过ps -p $$ | tail -n1 | awk '{print $NF}'在没有别名的 shell 中运行来检查这一点。如果输出不是bash,则您正在运行不同的 shell。也许您已将默认登录 shell 设置为sh,它dash在 Ubuntu 上。您可以使用 检查当前值echo $SHELL,也可以使用 更改它chsh


根据您的最新编辑,您的情况似乎是:

  1. 由于某种原因,您没有~/.profile。并且,根据您上次的编辑,情况似乎确实如此。因此,只需复制默认的.profile/etc/skel下次登录时应该就可以了:

    cp /etc/skel/.profile ~/
    

答案2

@terdon 说得对:出于某种原因,我的主目录中没有 .profile。之后一切都正常了 - 就像建议的那样 - cp /etc/skel/.profile ~

相关内容