[rps-include post=6835]
[rps-include post = 6835]
Linux and related distributions like Ubuntu, Debian, CentOS, Fedora, RedHat, … provides shell to manage systems. By default bash shell is used by the most of the distributions. Every shell in Linux have some configuration, environment variables, etc. We generally need to change them after opening a new shell. .bashrc
is used to set shell related configuration during new shell creation. In every new shell .bashrc
file is fired before we get the new shell. In this tutorial we will look how to use .bashrc
.
Linux和相关发行版(如Ubuntu,Debian,CentOS,Fedora,RedHat等)提供了用于管理系统的shell。 默认情况下,大多数发行版都使用bash shell。 Linux中的每个shell都有一些配置,环境变量等。我们通常需要在打开新的shell之后对其进行更改。 .bashrc
用于在创建新外壳时设置与外壳相关的配置。 在每个新的shell中,在我们获得新的shell之前都会触发.bashrc
文件。 在本教程中,我们将研究如何使用.bashrc
。
编辑.bashrc (Edit .bashrc)
.bashrc provides very flexible way to edit new shell configuration. Each user will have the .bashrc
file in their home directory. .bashrc
is a hidden file and not listed with ls
or File managers. But we can edit it just providing the path like below.
.bashrc提供了一种非常灵活的方式来编辑新的shell配置。 每个用户的主目录中都有.bashrc
文件。 .bashrc
是隐藏的文件,没有与ls
或文件管理器一起ls
。 但是我们可以编辑它,只需提供如下所示的路径即可。
Following command will open .bashrc of current user with nano text editor
以下命令将使用nano文本编辑器打开当前用户的.bashrc
$ nano ~/.bashrc
Which will look like below.
如下所示。

添加新路径变量 (Add New Path Variable)
One of the most popular use case for add new Path for the current user is adding to the .bashrc . In this example we will add path /opt/bin/
as new path for the current user.
当前用户添加新路径的最流行的用例之一是添加到.bashrc中。 在此示例中,我们将添加路径/opt/bin/
作为当前用户的新路径。
export PATH=$PATH:/opt/bin
添加别名 (Add Alias)
While running complex commands writing them again and again is redundant task. Alias can be used to create shortcuts in the command line. Another useful usage for .bashrc
is defining new alias for the current user. In this example we add alias for mv
command.
在运行复杂命令时,一次又一次地写入它们是多余的任务。 别名可用于在命令行中创建快捷方式。 .bashrc
另一个有用用法是为当前用户定义新别名。 在此示例中,我们为mv
命令添加别名。
alias mv='mv -i'
变更提示 (Change Prompt)
Linux provides prompt for each interactive shell. This generally provides the user name , host name and path. We may want to use more simple prompt just like $
we can set from .bashrc with the following command.
Linux为每个交互式Shell提供提示。 这通常提供用户名,主机名和路径。 我们可能希望使用更简单的提示符,就像$
可以使用以下命令从.bashrc设置的那样。
export PS1="$ "
源其他配置文件 (Source Other Configuration File)
We may need to load other configuration and shell startup file other than .bashrc. In this situations we can use .bashrc too.
我们可能需要加载.bashrc以外的其他配置和Shell启动文件。 在这种情况下,我们也可以使用.bashrc。
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
Bashrc和Bash_profile (Bashrc vs Bash_profile)
There is an alternative to bashrc file which is named bash_profile . bash_profile provides global configuration for all users. Bashrc is limited with current user.
bashrc文件还有一个替代文件,名为bash_profile。 bash_profile为所有用户提供全局配置。 Bashrc受当前用户限制。
重新加载Bashrc (Reload Bashrc)
Sometimes we need to reload bashrc file without logout and login. We can use source mechanism to reload bashrc. One of the following commands can be used to reload bashrc.
有时我们需要在不注销和登录的情况下重新加载bashrc文件。 我们可以使用源机制来重新加载bashrc。 以下命令之一可用于重新加载bashrc。
$ source ~/.bashrc
or
要么
$ . ~/.bashrc
[rps-include post=6835]
[rps-include post = 6835]
翻译自: https://www.poftut.com/linux-bashrc-file-and-usage-examples/