简介
在这个实验中,你将学习如何检查 Linux 中是否定义了 shell 别名(alias)。你将首先使用 alias
命令列出当前定义的别名,以了解什么是别名以及它们是如何显示的。
接下来,你将探索配置别名的常见位置。这包括检查通常存储在 ~/.bashrc
文件中的用户特定别名,以及查看 /etc/bash.bashrc
中的系统范围别名。在本实验结束时,你将能够在 Linux 环境中识别和定位别名定义。
在这个实验中,你将学习如何检查 Linux 中是否定义了 shell 别名(alias)。你将首先使用 alias
命令列出当前定义的别名,以了解什么是别名以及它们是如何显示的。
接下来,你将探索配置别名的常见位置。这包括检查通常存储在 ~/.bashrc
文件中的用户特定别名,以及查看 /etc/bash.bashrc
中的系统范围别名。在本实验结束时,你将能够在 Linux 环境中识别和定位别名定义。
在这一步中,你将了解 Linux 中的别名(alias),以及如何使用 alias
命令列出它们。
别名是一种快捷方式,允许你用一个更简短、更易记的命令来替代一个长命令。这可以为你节省大量的输入时间,并使你的命令行操作更加高效。
例如,每次你想要详细列出文件时,不必每次都输入 ls -l
,你可以创建一个像 ll
这样的别名来实现相同的功能。
要查看当前终端会话中设置的别名,只需输入 alias
命令并按回车键:
alias
你将看到一个别名列表,类似于以下内容:
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'
此输出在左侧显示别名名称(例如 ll
),在右侧显示它所代表的命令(例如 ls -alF
)。
注意 ls
别名。它被设置为 ls --color=auto
。这意味着每当你输入 ls
时,系统实际上执行的是 ls --color=auto
,该命令会为输出添加颜色,以提高可读性。
理解别名是自定义终端环境并使其更适合你使用的第一步。
点击 继续 进入下一步。
在上一步中,你使用 alias
命令查看了别名列表。现在,让我们来探究其中一些别名的定义位置。
用户特定的别名通常存储在你家目录(home directory)的配置文件中。对于使用 Bash shell 的用户来说,常见的存储位置是 ~/.bashrc
文件。尽管本实验环境使用的是 zsh
,但了解 ~/.bashrc
的作用很重要,因为它在许多 Linux 系统中被广泛使用。
~
符号是你家目录的快捷方式,在这个环境中,家目录是 /home/labex
。因此,~/.bashrc
指的是直接位于你家目录中的 .bashrc
文件。文件名开头的点号 (.
) 使其成为一个隐藏文件,这意味着除非你使用 -a
选项,否则简单的 ls
命令不会显示它。
让我们使用 cat
命令查看 ~/.bashrc
文件的内容。cat
用于显示文件内容。
输入以下命令并按回车键:
cat ~/.bashrc
你将看到 .bashrc
文件的内容。滚动输出内容,查找以 alias
开头的行。
## some example aliases
## alias ll='ls -l'
## alias la='ls -a'
## Add an "alert" alias for long running commands. Use like:
## sleep 10; alert
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 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
## enable programmable completion features (you don't need to enable
## this, if it's already enabled in /etc/bash.bashrc and /etc/profile
## sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
你可能会注意到,一些以 alias
开头的行被 #
注释掉了。这些是示例或未激活的别名。活动的别名是那些行首没有 #
的别名。
如果你使用的是 Bash,通常会在这个文件中添加你自己的自定义别名。例如,你可以添加一行 alias update='sudo apt update && sudo apt upgrade'
来创建一个用于更新系统的别名。
点击 继续 以继续探索系统范围的别名。
除了像 ~/.bashrc
这样的用户特定配置文件外,还有适用于系统上所有用户的系统范围配置文件。对于 Bash 来说,其中一个这样的文件是 /etc/bash.bashrc
。
这个文件通常包含系统管理员希望所有用户都能使用的默认设置和别名。
由于这个文件位于 /etc
目录中,该目录通常用于系统配置,你可能需要管理员权限才能修改它。不过,我们可以使用 cat
命令在无需特殊权限的情况下查看其内容。
让我们查看 /etc/bash.bashrc
文件的内容。输入以下命令并按回车键:
cat /etc/bash.bashrc
你将看到系统范围的 Bash 配置文件的内容。查找定义别名的行。
## System-wide .bashrc file for interactive bash(1) shells.
## To enable the setting of the locale environment variables see
## /etc/profile.d/locale.sh. By default in Ubuntu OnLine,
## this is done from /etc/profile.
## If not running interactively, don't do anything
[ -z "$PS1" ] && return
## check the window size after each command and, if necessary,
## update the values of LINES and COLUMNS.
shopt -s checkwinsize
## If set, the pattern "**" used in a pathname expansion context should
## match only directories and subdirectories in addition to the contents of
## the current directory.
#shopt -s globstar
## make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
## set variable identifying the chroot you work in (used in the prompt)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
## enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias ls='ls --color=auto'
#alias dir='dir --color=auto'
#alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
## colored GCC warnings and errors
#export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=(01;34):quote=01;32'
## some more ls aliases
#alias ll='ls -l' #alias ll='ls -alF'
#alias la='ls -a' #alias la='ls -AF'
#alias l='ls -CF'
## Add an alias for the 'alert' command
#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$//'\'')"'
## enable programmable completion features (you don't need to enable
## this, if it's already enabled in /etc/bash.bashrc and /etc/profile
## sources /etc/bash.bashrc).
if ! shopt -oq posix; then
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
fi
你应该会看到一些与使用 alias
命令时相同的别名,例如支持颜色显示的 ls
别名。这证实了你使用的一些默认别名是在系统范围内设置的。
了解别名的定义位置有助于你排查意外行为并有效地自定义你的环境。
你现在已经学会了如何列出活动别名以及检查存储它们的常见配置文件。
点击 继续 完成本实验。
在本实验中,你学习了如何检查 Linux 中是否定义了 shell 别名。你首先使用 alias
命令列出了当前终端会话中设置的别名,了解到别名是较长命令的快捷方式。
然后,你探索了别名的常见定义位置,特别关注了通常在 ~/.bashrc
文件中找到的用户特定别名,以及可以在 /etc/bash.bashrc
等文件中找到的系统范围别名。这个过程有助于你理解别名是如何配置的,以及在哪里可以找到它们的定义。