第 185 / 187 篇

17.14. 点文件与 .bashrc

说明

本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/Appendix2/dotfiles.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。

17.14. Dot Files and .bashrc

Unix 文件系统中有不少点文件和点目录。点文件(dot file)是名字以 . 字符开头的文件。这些文件通常是隐藏的,因为 ls 命令默认不列出它们。要查看这些隐藏文件,可以给 ls 加上 -a 命令行参数。例如:

There are a number of dot files and dot directories in the Unix filesystem. Dot files are files whose name starts with a . character. These files are typically hidden because the ls command does not list them by default. To view these hidden files, use the -a command line argument to ls. For example:

1
2
3
4
5
$ ls
unix_notes/

$ ls -a
. .. .bashrc .vimrc .xsession /unix_notes

每个目录中总有两个点目录:. 是该目录自身的别名,.. 是它父目录的别名。正如我们在第 17.1 节中看到的,. 和 .. 常用在相对路径名中。例如,.. 的一个常见用法是向上移动一级目录(把当前工作目录改为当前目录的父目录):

Two dot directories are always present in every directory: . is an alias for the directory itself, and .. is an alias to its parent directory. As we saw in Section 17.1, . and .. are often used in relative path names. For example, a common use of .. to move up one directory (to change the current working directory to be the parent of the current directory):

1
$ cd ..

. 这一项也常出现在运行当前目录中可执行文件的命令里,即在命令前加上前缀 ./。例如,要运行当前工作目录中的 a.out,用户应输入:

The . entry also commonly appears in commands that run an executable file from the current directory by prefixing the command with ./ For example, to run a.out in the current working directory, a user would type the following:

1
$ ./a.out

许多点文件是各种应用程序的配置文件(或存放配置文件的目录)。这些点文件包含应用程序特定的选项,可以改变应用程序的行为。程序在刚启动时会读取并解析它的点文件内容,以配置自己的行为。其中许多点文件可以由用户编辑,以定制他们所运行程序的行为。下面是这类点文件的几个例子:

Many dot files are configuration files (or directories of configuration files) for different applications. These dot files contain application-specific options that can change how the application behaves. Application dot file contents are evaluated by a program when it first starts up to configure its behavior. Many of these dot files can be edited by a user to customize the behavior of the programs they run. Here are a few examples of these types of dot files:

  • .bashrc:bash shell 程序的配置文件,用于设置环境变量、定义命令别名,……
  • .bashrc: a configuration file for the bash shell program set environment variables, define command aliases, …​
  • .vimrc:vim 编辑器的配置文件,用于定义配色和语法高亮方案、编码,……
  • .vimrc: a configuration for the vim editor define color and highlighting scheme, encoding, …​
  • .xsession:在终端上登录后启动的 X Window 管理器的配置文件,用于设置背景颜色、定义一些默认行为、启动某些应用程序,或启动某个特定的窗口管理器(它有自己的配置点文件)
  • .xsession: # a configuration file for the X Windows managers that are started after login on a terminal. set background color, define some default behavior start some applications, start a specific window manager (which has its own config dot files)

17.14.1. .bashrc 文件

17.14.1. The .bashrc File

Unix shell 配置文件(例如 Bash shell 的 .bashrc)包含改变 shell 行为的条目。通常,系统管理员会为系统设置一个默认的 .bashrc 文件。不过,个人用户常常希望进一步定制自己的 shell,他们可以通过编辑自己主目录中的 .bashrc 来做到这一点。

Unix shell configuration files (e.g., .bashrc for the Bash shell) contain entries that change the behavior of the shell. Typically a system administrator sets up a default .bashrc file for the system. However, individual users often want to customize their shell further, and they can do so by editing the .bashrc in their home directory.

原书注

注意
shell 在刚启动时读取 .bashrc 文件。因此,对 .bashrc 文件的修改不会生效,除非用户运行 source .bashrc 或重新启动 bash。

The shell reads the .bashrc file when it first starts up. Thus, changes to the .bashrc file do not take effect unless the user runs source .bashrc or restarts bash.

用户常做的两种修改是:改变 shell 环境变量的定义,以及添加命令别名(用户为自己常运行的命令设置的简写)。shell 环境变量(shell environment variable)被 shell 用来决定某些 shell 功能如何工作。要列出所有环境变量,可以用 env 命令;要列出某个特定环境变量的值,可以用 echo $env_var_name。例如:

Two common changes that users make are changing the definition of shell environment variables and adding command aliases (a user’s shorthand for commands that they commonly run). Shell environment variables are used by the shell to determine how certain shell features work. To list all the environment variables, use the env command, or list the value of a particular environment variable using echo $env_var_name. For example:

译注

示例中的 $env 应为 $ env($ 是提示符,后面应有空格),否则 shell 会把 $env 当作对变量 env 的引用。

1
2
3
4
5
6
7
8
9
$ echo $GROUP    # list your default unix file group
users

$env # list value of all env vars (a lot)
SHELL=/bin/bash
GROUP=users
...
PATH=/usr/bin:/usr/local/bin:/bin
...

PATH 环境变量特别有意思——它列出了一组目录,当用户在 shell 提示符下输入命令时,shell 会(按顺序)在这些目录中查找程序。例如,按照上面给出的 PATH 定义,如果用户输入 ls 命令,shell 会先在 /usr/bin 目录中查找它(shell 查找名为 ls 的可执行文件),找到就运行它。如果在 /usr/bin 中没找到 ls,shell 接着在 /usr/local/bin 中找;如果还没找到,再到 /bin 中找;如果仍然没找到,shell 就打印一条错误信息,说找不到 ls 命令。正是因为有 PATH 环境变量,用户在运行 ls 这样的常用命令时才不需要写出完整的路径名!

The PATH environment variable is particularly interesting—​it lists the set of directories in which the shell searches (in order) for programs when the user enters a command at the shell prompt. For example, given the definition of PATH shown above, if a user enter the ls command, the shell will first look in the /usr/bin directory for it (the shell looks for an executable file named ls) and runs it if found. If ls is not found in /usr/bin, then the shell next looks in /usr/local/bin, and if it is not found, it next look in /bin, and if it is still not found, the shell prints out error message saying that the ls command is not found. The PATH environment variable is the reason why users do not need to list the full path name to common commands, like ls, when they run them!

由于同一个程序可能在不同位置有多个版本,$PATH 中目录名的顺序决定了运行的是哪一个。用户可以用 which 命令查看自己运行的是程序的哪个版本。例如:

Because there may be multiple versions of the same program in different locations, the order of the directory names in $PATH determines which one is run. A user can see which version of a program they are running using the which command. For example:

1
2
$ which ls
/usr/bin/ls

用户还可以在自己的 .bashrc 文件中重新定义 PATH,以改变 Bash 搜索路径的顺序或添加额外的搜索路径。例如,如果 Sarita 自己编写了一些常用的实用程序,她可能想把它们放在主目录下一个名为 mybin 的目录中,然后更新她的 PATH 环境变量,把这个目录包括进去,这样她每次运行这些程序时就不必写出完整的路径名。她可以修改 bashrc(即 .bashrc)文件中已有的 PATH 定义,或者添加一条像下面这样的额外 PATH 定义:

Users can also redefine PATH in their .bashrc files to change the order of the paths searched by Bash or to add additional search paths. For example, if Sarita creates her own utility programs that she often runs, she may want to put them in a directory named mybin in her home directory and then update her PATH environment variable to include this directory so that she can run them without listing their full path name each time. She could do this by modifying the existing definition of PATH in her bashrc file or by adding an additional definition of PATH like the following:

1
PATH=$PATH:/home/sarita/mybin

这条定义把她的 mybin 目录追加到她现有 PATH 环境变量的末尾(把 PATH 设为 PATH 的当前值再加上另一个路径 /home/sarita/mybin)。

This definition appends her mybin directory to the end of her existing PATH environment variable (set PATH to the current value of PATH with another path called /home/sarita/mybin).

当 Sarita 启动一个新的 Bash shell,或者在当前运行的 Bash shell 中输入 source ~/.bashrc 时,她的路径就会更新,包含 /home/sarita/mybin/。

When Sarita starts a new Bash shell, or if she types source ~/.bashrc in a currently running Bash shell, her path will be updated to include /home/sarita/mybin/.

对 .bashrc 文件的另一种常见修改,是为常输入的命令添加简写别名,或者添加能确保命令总是带着特定命令行选项运行的别名。一个常见的例子是给命令 rm -i 起别名 rm。这样就能确保每当在命令行上输入 rm 命令时,shell 实际执行的是 rm -i,它在删除文件之前会提示用户确认是否真的要删除。下面是在 .bashrc 文件中为 rm 添加这个别名的方法:

Another common modification to a .bashrc file is to add shorthand aliases for either commonly typed commands or commands that ensure they are always run with specific command line options. A common example is to alias the command rm -i to be rm. This ensures that anytime the rm command is entered on the command line, the shell actually executes rm -i, which prompts the user to confirm that they really want to delete the file before removing it. Here is how to add this alias for rm in a .bashrc file:

1
alias rm="rm -i"      # add alias for rm to double check

类似地,如果 Sam 经常运行命令 cd ~/classes/CS31/,他可能想像这样在自己的 .bashrc 中为这条命令添加一个 Bash 简写别名:

Similarly, if Sam is frequently running the command cd ~/classes/CS31/, he may want to add a shorthand Bash alias for this command to his .bashrc like this:

1
alias gt31="cd ~/classes/CS31"  # goto (cd) into my CS31 directory

然后 Sam 就可以开始使用这些新别名了。例如:

Then Sam could start using these new aliases. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ source ~/.bashrc    # source: make bash re-evaluate .bashrc file

$ rm notes # rm is now an alias for rm -i
rm: remove regular file 'notes'? n
$ ls
classes/ letters/ notes projects/

$ rm notes
rm: remove regular file 'notes'? y
$ ls
classes/ letters/ projects/

$ gt31
$ pwd
/home/sam/classes/CS31

17.14.2. 参考资料

17.14.2. References

更多信息参见:

For more information see: