# 練習 3:Bash:Shell、`.profile`、`.bashrc`、`.bash_history`。
> 原文:[Exercise 3. Bash: The shell, .profile, .bashrc, .bash_history](https://archive.fo/DKP67)
> 譯者:[飛龍](https://github.com/wizardforcel)
> 協議:[CC BY-NC-SA 4.0](http://creativecommons.org/licenses/by-nc-sa/4.0/)
> 自豪地采用[谷歌翻譯](https://translate.google.cn/)
當使用 CLI(命令行界面)來使用 Linux 時,你正在與一個名為 shell 的程序進行交互。所有你輸入的都傳遞給 shell,它解釋你輸入的內容,執行參數擴展(這有點類似于代數中的花括號擴展),并為你執行程序。我們將使用的 Shell 稱為 Bash,它代表 Bourne Again Shell,而 Bourne Again Shell 又是一個雙關語。現在我將使用純中文,向大家介紹一下 bash 的工作方式:
+ 你
+ 登入 Linux 虛擬機
+ 你的身份由用戶名(`user1`)和密碼(`123qwe`)確定。
+ Bash 執行了。
+ Bash
+ 從你的配置中讀取并執行首個命令,它定義了:
+ 命令提示符是什么樣子
+ 使用 Linux 時,你會看到什么顏色
+ 你的編輯器是什么
+ 你的瀏覽器是什么
+ ...
+ 讀取首個命令后,Bash 進入循環
+ 沒有通過輸入`exit`或者按下`<CTRL+D>`,來要求退出的時候:
+ 讀取一行
+ 解析這一行,擴展花括號
+ 使用擴展參數執行命令
我重復一下,你輸入的任何命令都不會直接執行,而是首先擴展,然后執行。例如,當你輸入`ls *`時,星號`*`將擴展為當前目錄中所有文件的列表。
現在你將學習如何修改你的配置,以及如何編寫和查看你的歷史記錄。
## 這樣做
```
1: ls -al
2: cat .profile
3: echo Hello, $LOGNAME!
4: cp -v .profile .profile.bak
5: echo 'echo Hello, $LOGNAME!' >> .profile
6: tail -n 5 .profile
7: history -w
8: ls -altr
9: cat .bash_history
10: exit
```
## 你會看到什么
```
user1@vm1's password:
Linux vm1 2.6.32-5-amd64 #1 SMP Sun May 6 04:00:17 UTC 2012 x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Jun 7 12:03:29 2012 from sis.site
Hello, user1!
user1@vm1:~$ ls -al
total 20
drwxr-xr-x 2 user1 user1 4096 Jun 7 12:18 .
drwxr-xr-x 3 root root 4096 Jun 6 21:49 ..
-rw-r--r-- 1 user1 user1 220 Jun 6 21:48 .bash_logout
-rw-r--r-- 1 user1 user1 3184 Jun 6 21:48 .bashrc
-rw-r--r-- 1 user1 user1 697 Jun 7 12:04 .profile
user1@vm1:~$ cat .profile
# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.
# the default umask is set in /etc/profile; for setting the umask
# for ssh logins, install and configure the libpam-umask package.
#umask 022
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
echo Hello, $LOGNAME!
user1@vm1:~$ echo Hello, $LOGNAME!
Hello, user1!
user1@vm1:~$ cp -v .profile .profile.bak
`.profile' -> `.profile.bak'
user1@vm1:~$ echo 'echo Hello, $LOGNAME!' >> .profile
user1@vm1:~$ tail -n 5 .profile
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
echo Hello, $LOGNAME!
user1@vm1:~$ history -w
user1@vm1:~$ ls -altr
total 28
-rw-r--r-- 1 user1 user1 3184 Jun 6 21:48 .bashrc
-rw-r--r-- 1 user1 user1 220 Jun 6 21:48 .bash_logout
drwxr-xr-x 3 root root 4096 Jun 6 21:49 ..
-rw-r--r-- 1 user1 user1 741 Jun 7 12:19 .profile.bak
-rw------- 1 user1 user1 308 Jun 7 12:21 .bash_history
-rw-r--r-- 1 user1 user1 697 Jun 7 12:25 .profile
drwxr-xr-x 2 user1 user1 4096 Jun 7 12:25 .
user1@vm1:~$ cat .bash_history
ls -al
cat .profile
echo Hello, $LOGNAME!
cp -v .profile .profile.bak
echo 'echo Hello, $LOGNAME!' >> .profile
tail -n 5 .profile
history -w
ls -altr
user1@vm1:~$ exit
logout
```
不要害怕,所有命令都會解釋。行號對應“現在輸入它”的部分。
## 解釋
1. 打印當前目錄中的所有文件,包括隱藏的文件。選項`-al`告訴`ls` 以`long`格式打印文件列表,并包括所有文件,包括隱藏文件。`.profile`和`.bash_rc`是隱藏文件,因為它們以點`.`開頭。以點開頭的每個文件都是隱藏的,這很簡單。這兩個特殊文件是 shell 腳本,它們包含登錄時執行的指令。
2. 打印出你的`.profile`文件。只是這樣。
3. 告訴你的 shell,你這里是 bash,輸出一個字符串`Hello, $LOGNAME!`,用環境變量``$LOGNAME`替換$LOGNAME`,它包含你的登錄名。
4. 將`.profile`文件復制到`.profile.bak`。選項`-v`讓`cp`詳細輸出,這意味著它會打印所有的操作。記住這個選項,它通常用于讓命令給你提供比默認更多的信息。
5. 在`.bash_rc`配置文件中添加一行。從現在開始,每次登錄到`vm1`時, 都將執行該命令。注意,`>>`代表向文件添加了一些東西,但`>`意味著使用一些東西來替換文件。如果你不小心替換了`.profile`而不是向它添加,則命令
```
cp -v .profile.bak .profile
```
會向你返回舊的`.profile`文件。
6. 從`.profile`文件中精確打印出最后 5 行。
7. 將所有命令歷史寫入`.bash_history`文件。通常這是在會話結束時完成的,當你通過鍵入`exit`或按`<CTRL> + D`關閉它。
8. 打印當前目錄中的文件。選項`-tr`表示文件列表按時間反向排序。這意味著最近創建和修改的文件最后打印。注意你現在有兩個新的文件。
9. 打印出保存命令歷史記錄的文件。注意你所有的輸入都在這里。
0. 關閉會話
## 附加題
+ 在線搜索為什么`ls -al`告訴你“總共 20”,但是只有 5 個文件存在。 這是什么意思? 請注意,`.`和`..`是特殊文件條目,分別對應于當前目錄和父目錄的。
+ 登錄`vm1`并鍵入`man -K /etc/profile`,現在使用光標鍵滾動到`INVOCATION`部分并閱讀它。 要退出`man`,請鍵入`q`。 鍵入`man man`來找出`man -K`選項的含義。
+ 在命令之前鍵入`uname`與空格。 現在,鍵入`history`。 看到了嗎?如果你將空格放到命令前面,則不會將其保存在歷史記錄中!提示:當你需要在命令行上指定密碼時,很實用。
+ 找到 bash 的 wiki 頁面,并嘗試閱讀它。不用擔心,如果它嚇到你,只需要省略可怕的部分。
- 笨辦法學 Linux 中文版
- 練習 0:起步
- 練習 1:文本編輯器,vim
- 練習 2:文本瀏覽器,少即是多
- 練習 3:Bash:Shell、.profile、.bashrc、.bash_history
- 練習 4:Bash:處理文件,pwd,ls,cp,mv,rm,touch
- 練習 5:Bash:環境變量,env,set,export
- 練習 6:Bash:語言設置,LANG,locale,dpkg-reconfigure locales
- 練習 7:Bash:重定向,stdin,stdout,stderr,<,>,>>,|,tee,pv
- 練習 8:更多的重定向和過濾:head,tail,awk,grep,sed
- 練習 9:Bash:任務控制,jobs,fg
- 練習 10:Bash:程序退出代碼(返回狀態)
- 練習 11:總結
- 練習 12:文檔:man,info
- 練習 13:文檔:Google
- 練習 14:包管理:Debian 包管理工具aptitude
- 練習 15:系統啟動:運行級別,/etc/init.d,rcconf,update-rc.d
- 練習 16:處理進程,ps,kill
- 練習 17:任務調度:cron,at
- 練習 18:日志:/var/log,rsyslog,logger
- 練習 19:文件系統:掛載,mount,/etc/fstab
- 練習 20:文件系統:修改和創建文件系統,tune2fs,mkfs
- 練習 21:文件系統:修改根目錄,chroot
- 練習 22:文件系統:移動數據,tar,dd
- 練習 23:文件系統:權限,chown,chmod,umask
- 練習 24:接口配置,ifconfig,netstat,iproute2,ss,route
- 練習 25:網絡:配置文件,/etc/network/interfaces
- 練習 26:網絡:封包過濾配置,iptables
- 練習 27:安全 Shell,ssh,sshd,scp
- 練習 28:性能:獲取性能情況,uptime,free,top
- 練習 29:內核:內核消息,dmesg
- 練習 30:打磨、洗練、重復:總復習
- 下一步做什么
- Debian 手動安裝