8.6 管道符和作业控制
8.7/8.8 shell变量
8.9 环境变量配置文件
扩展 bashrc和bash_profile的区别 http://ask.apel
8.6 管道符和作业控制
管道符|
管道符|用于将前一个指令的输出作为后一个指令的输入。
统计1.txt的行数。
[root ~]# cat 1.txt | wc -l
2统计/root目录下的文件和目录(不包括隐藏的)个数。 [root ~]# ls | wc -l
11列出当前目录下的文件(包括隐藏文件)个数[root ~]# find ./ -type f | wc -l
19 [root ~]#作业控制
当进行进程时,你可以使它暂停(按Ctrl+Z组合键),然后使用fg(foreground的简写)命令恢复它,或是利用bg(background的简写)命令使它到后台运行。此外,你也可以使它终止(按Ctrl+C组合键)。
使用vi命令编辑test1.txt,随便输入一些内容,按Esc键后,使用Ctrl+Z组合键暂停任务。此时提示test1.txt已经停止了,然后使用fg命令恢复它,此时又进入刚才的vi窗口了。再次使其暂停,然后输入jobs,可以看到被暂停或者在后台运行的任务。如果想把暂停的任务放在后台重新运行,就是用bg命令。多个被暂停的任务会有编号,使用jobs命令可以看到两个任务,使用bg命令或者fg命令时,则需要在后面加上编号。
如何关掉在后台运行的任务呢?如果你没有退出刚才的shell,那么应该先使用命令fg编号把任务调到前台,然后Ctrl+C组合键结束任务。另一种情况,关闭当前的shell,再次打开另一个shell时,使用jobs命令并不会显示在后台运行或者被暂停的任务。要想关闭这些任务,则需要先知道它们的pid。使用&把任务放到后台运行时,会显示pid信息。如果忘掉这个pid,还可以使用ps aux命令找到那个进程。如果想结束该进程,需要使用kill命令。kill命令很简单,直接在后面加pid即可。如果遇到结束不了的进程,可以在kill后面加一个选项,即kill -9 [pid]。
ctrl z 临时暂停一个任务,然后就可以退回到命令窗口做其他事情。
[root ~]# vim 1.txt
[1]+ Stopped vim 1.txt
[root@localhost ~]#fg[id]把任务调到前台,支持多个任务
[root@localhost ~]# vim 2.txt
[2]+ Stopped vim 2.txt
[root@localhost ~]# fg 1 vim 1.txt[1]+ Stopped vim 1.txt
jobs查看后台的任务,可以把停止的或者已经丢到后台的任务列出来
[root@localhost ~]# jobs
[1]+ Stopped vim 1.txt [2]- Stopped vim 2.txtbg [id]把任务调到后台,就是在后台运行起来。
[root@localhost ~]# bg 2
[2]- vim 2.txt &[2]+ Stopped vim 2.txt
[root@localhost ~]# jobs
[1]- Stopped vim 1.txt [2]+ Stopped vim 2.txt[root@localhost ~]# fg 2 vim 2.txt保存退出。
[root@localhost ~]# jobs [1]+ Stopped vim 1.txtvmstat命令查看系统的运行状况
[root@localhost ~]# vmstat 1 procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 1193296 2084 373572 0 0 85 7 79 84 0 0 98 1 0 0 0 0 1193296 2084 373604 0 0 0 0 73 76 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 6 126 134 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 149 147 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 113 112 0 1 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 121 125 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 120 121 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 123 127 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 108 105 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 118 126 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 120 119 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 121 124 0 0 100 0 0 0 0 0 1193296 2084 373604 0 0 0 0 110 109 0 1 100 0 0 0 0 0 1193172 2084 373604 0 0 0 0 180 165 0 0 100 0 0 0 0 0 1193172 2084 373604 0 0 0 0 124 125 0 0 100 0 0 0 0 0 1193172 2084 373604 0 0 0 0 120 124 0 0 100 0 0 0 0 0 1193172 2084 373604 0 0 0 0 113 115 0 0 100 0 0 0 0 0 1193172 2084 373604 0 0 0 0 108 113 0 0 100 0 0 ^Z [2]+ Stopped vmstat 1bg将命令调回后台,fg将命令调回前台,ctrl c终止一个命令。
命令sleep +n表示停止n秒。
[root@localhost ~]# sleep 1000
^Z [1]+ Stopped sleep 1000[root@localhost ~]# jobs [1]+ Stopped sleep 1000[root@localhost ~]# sleep 200
^Z [2]+ Stopped sleep 200[root@localhost ~]# jobs [1]- Stopped sleep 1000 [2]+ Stopped sleep 200fg不加数字,代表调最近的一个暂停的程序到前台。
[root@localhost ~]# fg
sleep 200 ^Z [2]+ Stopped sleep 200bg 加数字,代表将这个程序丢到后台并运行。
[root@localhost ~]# bg 1
[1]- sleep 1000 &[root@localhost ~]# jobs [1]- Running sleep 1000 & [2]+ Stopped sleep 200[root@localhost ~]# fg 1
sleep 1000 ^C[root@localhost ~]# jobs
[2]+ Stopped sleep 200[root@localhost ~]# fgsleep 200 [root@localhost ~]# jobs [root@localhost ~]#命令后面加&直接丢到后台
[root@localhost ~]# sleep 100 &
[1] 3359[root@localhost ~]# jobs [1]+ Running sleep 100 & [root@localhost ~]#ps aux命令用于查看进程。
[root@localhost ~]# ps aux | grep sleep
root 3359 0.0 0.0 107904 612 pts/0 S 19:50 0:00 sleep 100 root 3367 0.0 0.0 107904 608 ? S 19:51 0:00 sleep 60 root 3369 0.0 0.0 112660 968 pts/0 S+ 19:52 0:00 grep --color=auto sleep [root@localhost ~]#8.7/8.8 shell变量
环境变量PATH,它是shell预设的一个变量。通常,shell预设的变量都是大写的。变量就是使用一个较简单的字符串来代替某些具有特殊意义的设定以及数据。就拿PATH来讲,这个PATH就代替了所有常用命令的绝对路径的设定。有了PATH这个变量,我们运行某个命令时,就不需要再输入全局路径,直接输入命令名即可。变量有PATH,HOME,PWD,LOGNAME,可以通过env命令来获取到这些变量。
使用env命令,可列出系统预设的全部系统变量。
登陆不同的用户,这些环境变量的值也不同。
HOSTNAME:表示主机名称
SHELL:当前用户的shell类型
HISTSIZE:历史记录数
MAIL:当前用户的邮件存放目录
PATH:该变量决定了shell将到哪些目录中寻找命令或者程序
PWD:当前目录
LANG:这是与语言相关的环境变量,多语言环境可以修改此环境变量
HOME:当前用户的家目录
LOGNAME:当前用户的登录名
env命令显示的变量只是环境变量,系统预设的变量还有很多,可以使用set命令把系统预设的全部变量都显示出来。
set命令多了很多变量,并且包括用户自定义的变量
set命令不仅可以显示系统预设的变量,也可以显示用户自定义的变量。
[root@localhost ~]# a=111 [root@localhost ~]# echo $a
111 [root@localhost ~]#这个变量和系统变量不太一样,是用户自定义的,不是系统内置的。
虽然可以自定义变量,但是该变量只能在当前shell中生效。使用bash命令可以再打开一个shell,此时先前设置的变量已经不存在了,退出当前的shell回到原来的bash,设定的变量还在。
想让设置的变量环境一直有效的两种情况如下:
情况一:
允许系统内所有用户登录后都能使用该变量。具体方法:在/etc/profile文件的最后一行加入export myname=Aming,然后运行source /etc/profile就可以生效了。此时再运行bash或者切换到其他账户就可以看到效果了。
情况二:
仅允许当前用户使用该变量。具体方法:再用户主目录下的.bashrc文件的最后一行加入
export myname=Aming,然后运行source .bashrc就可以生效了。此时再运行bash就可以看到效果了。
这里source的作用是将目前设定的配置刷新,即不用注销再登陆也能生效。
env和set命令不用过多纠结。
变量的定义规则
在linux下自定义设置变量的规则如下:
1、设定变量的格式为a=b,其中a为变量名,b为变量的内容,等号两边不能有空格。
2、变量名只能由字母、数字以及下划线组成,而且不能以数字开头。
3、当变量内容带有特殊符号(如空格)时,需要加上单引号。
4、当变量内容中本身带有单引号时,此时就需要加双引号了。
5、当变量内容需要用到其他命令时,运行结果则可以使用反引号。
[root@localhost ~]# a1=2 [root@localhost ~]# echo $a1
2[root@localhost ~]# a_1=3 [root@localhost ~]# echo $a_1 3[root@localhost ~]# _a1=4 [root@localhost ~]# echo $_a1 4[root@localhost ~]# 1aa=2
bash: 1aa=2: command not found...[root@localhost ~]# a='a b c' [root@localhost ~]# echo $a
a b c[root@localhost ~]# a="a b c" [root@localhost ~]# echo $a
a b c [root@localhost ~]#建议用单引号,双引号无法保证正确性。
[root@localhost ~]# a="a$bc" [root@localhost ~]# echo $a
a[root@localhost ~]# a='a$bc' [root@localhost ~]# echo $a a$bc [root@localhost ~]#变量内容可以累加其他变量的内容,但需要加上双引号。如果把双引号错加为单引号,则不能得到想要的内容。
[root@localhost ~]# a=1 [root@localhost ~]# b=2 [root@localhost ~]# echo $a$b
12[root@localhost ~]# a='a$bc' [root@localhost ~]# echo $a$ba$bc2[root@localhost ~]# c="a$b"c [root@localhost ~]# echo $c
a2c[root@localhost ~]# c='a$b'c [root@localhost ~]# echo $c
a$bc [root@localhost ~]#双引号和单引号的区别:使用双引号时,不会取消双引号中特殊符号本身的作用;而使用单引号时,里面的特殊字符将全部失去其本身的作用。
如在当前shell中运行bash命令,则会进入一个新的shell,这个shell就是原来shell的子shell。用pstree命令可以查看,如果pstree命令没有安装,可以通过yum install –y psmisc命令安装。pstree会把linux系统中的所有进程以树形结构显示出来。在父shell中设定变量后,进入子shell时,该变量是不会生效的。如果想让这个变量在子shell中生效,则要用到export指令。
其实export的作用就是声明一下这个变量,让该shell的子shell也知道该变量的值。设置变量之后,如果想取消这个变量,只要输入unset 变量名即可。
怎么查看自己在哪个终端下呢?
[root@localhost ~]# w
21:25:16 up 3:13, 1 user, load average: 0.14, 0.05, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.231.1 18:13 4.00s 0.19s 0.04s w[root@localhost ~]# echo $SSH_TTY /dev/pts/0打开另一个终端,查看具体在哪个终端下。
[root@localhost ~]# w
21:28:46 up 3:16, 2 users, load average: 0.46, 0.20, 0.11 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.231.1 18:13 2:46 0.15s 0.15s -bash root pts/2 192.168.231.1 21:28 6.00s 0.05s 0.01s w[root@localhost ~]# echo $SSH_TTY /dev/pts/2 [root@localhost ~]#在终端1定义aming=linux,在终端2上是查看不到的。
[root@localhost ~]# aming=linux [root@localhost ~]# echo $aming
linux [root@localhost ~]#[root@localhost ~]# echo $aming #终端2下无法查看
[root@localhost ~]#
在终端1中,再进入一个shell,也就是子shell。
[root@localhost ~]# bash [root@localhost ~]# w
21:33:22 up 3:21, 2 users, load average: 0.08, 0.09, 0.08 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.231.1 18:13 2.00s 0.19s 0.00s w root pts/2 192.168.231.1 21:28 2:10 0.04s 0.04s -bash [root@localhost ~]#[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}] ├─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─abrt-dbus───2*[{abrt-dbus}] ├─2*[abrt-watch-log] ├─abrtd ├─accounts-daemon───2*[{accounts-daemon}] ├─agetty ├─alsactl ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon} │ └─3*[{at-spi-bus-laun}] ├─at-spi2-registr───2*[{at-spi2-registr}] ├─atd ├─auditd─┬─audispd─┬─sedispatch │ │ └─{audispd} │ └─{auditd} ├─avahi-daemon───avahi-daemon ├─bluetoothd ├─chronyd ├─colord───2*[{colord}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─dbus-launch ├─dnsmasq───dnsmasq ├─firewalld───{firewalld} ├─gdm─┬─X───3*[{X}] │ ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}] │ │ │ ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}] │ │ │ │ │ ├─ibus-engine-sim───2*[{ibus-engine-sim}] │ │ │ │ │ └─2*[{ibus-daemon}] │ │ │ │ └─10*[{gnome-shell}] │ │ │ └─3*[{gnome-session-b}] │ │ └─2*[{gdm-session-wor}] │ └─3*[{gdm}] ├─gssproxy───5*[{gssproxy}] ├─ibus-x11───2*[{ibus-x11}] ├─irqbalance ├─ksmtuned───sleep ├─libvirtd───15*[{libvirtd}] ├─lsmd ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mcelog ├─packagekitd───2*[{packagekitd}] ├─pcscd───2*[{pcscd}] ├─polkitd───5*[{polkitd}] ├─pulseaudio───2*[{pulseaudio}] ├─rngd ├─rsyslogd───2*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─smartd ├─sshd───sshd─┬─bash───bash───pstree #我们在这里 │ └─bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] ├─upowerd───2*[{upowerd}] ├─vmtoolsd───{vmtoolsd} ├─wpa_supplicant └─xdg-permission-───2*[{xdg-permission-}] [root@localhost ~]#在子shell中查看变量aming,也是没有生效的。
[root@localhost ~]# echo $aming
[root@localhost ~]#
这个变量aming只在上面那个shell中生效,现在退出子shell。
[root@localhost ~]# exit
exit[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}] ├─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─2*[abrt-watch-log] ├─abrtd ├─accounts-daemon───2*[{accounts-daemon}] ├─agetty ├─alsactl ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon} │ └─3*[{at-spi-bus-laun}] ├─at-spi2-registr───2*[{at-spi2-registr}] ├─atd ├─auditd─┬─audispd─┬─sedispatch │ │ └─{audispd} │ └─{auditd} ├─avahi-daemon───avahi-daemon ├─bluetoothd ├─chronyd ├─colord───2*[{colord}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─dbus-launch ├─dnsmasq───dnsmasq ├─firewalld───{firewalld} ├─gdm─┬─X───3*[{X}] │ ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}] │ │ │ ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}] │ │ │ │ │ ├─ibus-engine-sim───2*[{ibus-engine-sim}] │ │ │ │ │ └─2*[{ibus-daemon}] │ │ │ │ └─10*[{gnome-shell}] │ │ │ └─3*[{gnome-session-b}] │ │ └─2*[{gdm-session-wor}] │ └─3*[{gdm}] ├─gssproxy───5*[{gssproxy}] ├─ibus-x11───2*[{ibus-x11}] ├─irqbalance ├─ksmtuned───sleep ├─libvirtd───15*[{libvirtd}] ├─lsmd ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mcelog ├─packagekitd───2*[{packagekitd}] ├─pcscd───2*[{pcscd}] ├─polkitd───5*[{polkitd}] ├─pulseaudio───2*[{pulseaudio}] ├─rngd ├─rsyslogd───2*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─smartd ├─sshd───sshd─┬─bash───pstree #现在在这里 │ └─bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] ├─upowerd───2*[{upowerd}] ├─vmtoolsd───{vmtoolsd} ├─wpa_supplicant └─xdg-permission-───2*[{xdg-permission-}] [root@localhost ~]#这种变量叫本地变量,也叫local,只在当前终端下生效,那如何变成全局变量呢?
[root@localhost ~]# export aming=linux [root@localhost ~]# echo $aming
linux[root@localhost ~]# bash [root@localhost ~]# echo $aming linux[root@localhost ~]# bash [root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}] ├─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─abrt-dbus───2*[{abrt-dbus}] ├─2*[abrt-watch-log] ├─abrtd ├─accounts-daemon───2*[{accounts-daemon}] ├─alsactl ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon} │ └─3*[{at-spi-bus-laun}] ├─at-spi2-registr───2*[{at-spi2-registr}] ├─atd ├─auditd─┬─audispd─┬─sedispatch │ │ └─{audispd} │ └─{auditd} ├─avahi-daemon───avahi-daemon ├─bluetoothd ├─chronyd ├─colord───2*[{colord}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─dbus-launch ├─dnsmasq───dnsmasq ├─firewalld───{firewalld} ├─gdm─┬─X───3*[{X}] │ ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}] │ │ │ ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}] │ │ │ │ │ ├─ibus-engine-sim───2*[{ibus-engine-sim}] │ │ │ │ │ └─2*[{ibus-daemon}] │ │ │ │ └─10*[{gnome-shell}] │ │ │ └─3*[{gnome-session-b}] │ │ └─2*[{gdm-session-wor}] │ └─3*[{gdm}] ├─gssproxy───5*[{gssproxy}] ├─ibus-x11───2*[{ibus-x11}] ├─irqbalance ├─ksmtuned───sleep ├─libvirtd───15*[{libvirtd}] ├─login───bash ├─lsmd ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mcelog ├─packagekitd───2*[{packagekitd}] ├─pcscd───2*[{pcscd}] ├─polkitd───5*[{polkitd}] ├─pulseaudio───2*[{pulseaudio}] ├─rngd ├─rsyslogd───2*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─smartd ├─sshd───sshd───bash───bash───bash───pstree #现在有两个子shell ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] ├─upowerd───2*[{upowerd}] ├─vmtoolsd───{vmtoolsd} ├─wpa_supplicant └─xdg-permission-───2*[{xdg-permission-}] [root@localhost ~]#重新打开一个终端,发现如下情况:
[root@localhost ~]# pstree
systemd─┬─ModemManager───2*[{ModemManager}] ├─NetworkManager───2*[{NetworkManager}] ├─VGAuthService ├─abrt-dbus───2*[{abrt-dbus}] ├─2*[abrt-watch-log] ├─abrtd ├─accounts-daemon───2*[{accounts-daemon}] ├─alsactl ├─at-spi-bus-laun─┬─dbus-daemon───{dbus-daemon} │ └─3*[{at-spi-bus-laun}] ├─at-spi2-registr───2*[{at-spi2-registr}] ├─atd ├─auditd─┬─audispd─┬─sedispatch │ │ └─{audispd} │ └─{auditd} ├─avahi-daemon───avahi-daemon ├─bluetoothd ├─chronyd ├─colord───2*[{colord}] ├─crond ├─cupsd ├─2*[dbus-daemon───{dbus-daemon}] ├─dbus-launch ├─dnsmasq───dnsmasq ├─firewalld───{firewalld} ├─gdm─┬─X───3*[{X}] │ ├─gdm-session-wor─┬─gnome-session-b─┬─gnome-settings-───5*[{gnome-settings-}] │ │ │ ├─gnome-shell─┬─ibus-daemon─┬─ibus-dconf───3*[{ibus-dconf}] │ │ │ │ │ ├─ibus-engine-sim───2*[{ibus-engine-sim}] │ │ │ │ │ └─2*[{ibus-daemon}] │ │ │ │ └─10*[{gnome-shell}] │ │ │ └─3*[{gnome-session-b}] │ │ └─2*[{gdm-session-wor}] │ └─3*[{gdm}] ├─gssproxy───5*[{gssproxy}] ├─ibus-x11───2*[{ibus-x11}] ├─irqbalance ├─ksmtuned───sleep ├─libvirtd───15*[{libvirtd}] ├─login───bash ├─lsmd ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mcelog ├─packagekitd───2*[{packagekitd}] ├─pcscd───2*[{pcscd}] ├─polkitd───5*[{polkitd}] ├─pulseaudio───2*[{pulseaudio}] ├─rngd ├─rsyslogd───2*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─smartd ├─sshd─┬─sshd───bash───bash───bash │ └─sshd───bash───pstree #现在在这里 ├─systemd-journal ├─systemd-logind ├─systemd-udevd ├─tuned───4*[{tuned}] ├─upowerd───2*[{upowerd}] ├─vmtoolsd───{vmtoolsd} ├─wpa_supplicant └─xdg-permission-───2*[{xdg-permission-}] [root@localhost ~]#在这个终端下,变量aming也不生效。
[root@localhost ~]# echo $aming
[root@localhost ~]#
原因在于这两个终端在两个sshd下面,没有冲突,没有交互。
在第一个终端下的子shell中设置全局变量b,看看在父shell中是否生效。
[root@localhost ~]# export b=123
[root@localhost ~]# echo $b
[root@localhost ~]# 123
[root@localhost ~]# exit
[root@localhost ~]# echo $b
[root@localhost ~]#
子shell设置的全局变量在父shell中是不生效的,所谓的全局变量是向下的。在当前shell中设置全局变量,它的子shelll,子子shell,……等等都是生效的。
变量可以定义,也可以取消,使用unset命令。
[root@localhost ~]# echo $aming
linux[root@localhost ~]# unset aming [root@localhost ~]# echo $aming[root@localhost ~]#
上面讲的那么多,都是export设置全局变量的用法,在子shell中才能发挥作用。
8.9 环境变量配置文件
这部分内容有难度,但是在实际工作中用的不是很多。
•/etc/profile 用户环境变量,交互,登录才执行
• /etc/bashrc 用户不用登录,执行shell就生效
• ~/.bashrc
• ~/.bash_profile
• ~/.bash_history
• ~/.bash_logout
• PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
上面的文件分两个维度,一个是系统层面,一个是用户层面的。
系统层面的就是?/etc下的文件,例如/etc/profile和/etc/bashrc。
用户层次的是用户家目录下的文件 ,例如.bash_profile、.bashrc、.bash_history、.bash_logout等等。
文件的名字很类似,可以把profile和bashrc各看成一种类型。这两种类型的区别是:profile是用户登录的时候会加载到,而bashrc是用户或者系统执行一些shell脚本的时候。比如打开一个shell,就会去执行/etc/bashrc的变量或者配置等等。
系统的文件/etc/profile和/etc/bashrc一般不要去编辑,当遇到需求需要编辑的时候,可以去编辑用户家目录下的,也就是用户自己的,这是可以的。
[root@localhost ~]# vim .bash_profile
# .bash_profile
# Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
这里可以定义一下PATH,这里只是针对用户生效,如果想要全局生效,就放到/etc/profile下面去。
/etc/profile:这个文件预设了几个重要的变量,例如PATH、USER、LOGNAME、MAIL、INPUTRC、HOSTNAME、HISTSIZE、umask
/etc/bashrc:这个文件主要预设umask以及PS1,这个PS1就是我们在输入命令时前面的那串字符。
\u指用户,\h指主机名,\W指当前目录,\$指字符#(如果是普通目录,则显示$),上面两个是系统文件,各个用户的主目录下还有以下几个隐藏文件。
.bash_profile:该文件定义了用户的个人化路径与环境变量的文件名称。每个用户都可以使用该文件输入专属于自己的shell信息,当用户登录时,该文件仅仅执行一次。
.bashrc:该文件包含专属于自己的shell的bash信息,当登录或每次打开新的shell时,该文件会被读取。
.bash_history:该文件用于记录命令历史。
.bash_logout:当退出shell时,会执行该文件。你可以将一些清理的工作放到这个文件中。
它定义用户退出的时候需要做的一些操作,比如说你想每次退出时都删除命令历史,你就可以把删除命令历史的命令放到.bash_logout里面去。
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
PS1是在/etc/bashrc里面定义的,在登陆系统以后,会出现[root@localhost ~]# 。
最左侧是root,也就是用户登录的名字。localhost是主机名,然后是你所在的目录最后一层级。
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]#[root@localhost network-scripts]# echo $PS1
[\u@\h \W]\$ [root@localhost network-scripts]#u是用户,h是主机名,W是目录的最后一个层级,这里的W是可以改成w的。
[root@localhost network-scripts]# PS1='[\u@\h \w]\$'
[root@localhost /etc/sysconfig/network-scripts]#此时换成了绝对路径。
[root@localhost ~]#ls
123 1.txt 2.txt 3.txt 456 789 anaconda-ks.cfg.1 a.txt bb.txt initial-setup-ks.cfg perl5[root@localhost ~]#cd 123 [root@localhost ~/123]#方括号不加也是可以的。
[root@localhost /tmp]#PS1='\u@\h \w\$'
root@localhost /tmp#方括号换成尖括号也是可以的。
root@localhost /tmp#PS1='<\u@\h \w>\$'
<root@localhost /tmp>#也可以让前面的整体带颜色显示。
<root@localhost /tmp>#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '
root@localhost:/tmp#那PS2是多少呢?
root@localhost:~# echo $PS2
>root@localhost:~# for i in `seq 1 100` > do > echo $i > done 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 root@localhost:~#root@localhost:~# PS2="#" root@localhost:~# echo $PS2
#root@localhost:~# for i in `seq 1 10` #do #echo $i #done 1 2 3 4 5 6 7 8 9 10 root@localhost:~#PS2一般不会动它,改回来即可。
友情链接: