[root@localhost ~]# ls 111 123 1.txt 234 2.txt 2.txt.bak 3.txt anaconda-ks.cfg [root@localhost ~]# ls | wc -l 8
[root@localhost ~]# find ./ -type f ./.bash_logout ./.bash_profile ./.bashrc ./.cshrc ./.tcshrc ./anaconda-ks.cfg ./.bash_history ./.viminfo ./1.txt ./2.txt ./3.txt ./2.txt.bak [root@localhost ~]# find ./ -type f |wc -l 计算当前目录下,有多少个文件 12
[root@localhost ~]# vim 1.txt [1]+ 已中止 vim 1.txt [root@localhost ~]# fg vim 1.txt [1]+ 已中止 vim 1.txt [root@localhost ~]# jobs [1]+ 已中止 vim 1.txt [root@localhost ~]# vim 2.txt [2]+ 已中止 vim 2.txt [root@localhost ~]# jobs [1]- 已中止 vim 1.txt [2]+ 已中止 vim 2.txt [root@localhost ~]#
[root@localhost ~]# fg 1
[root@localhost ~]# bg 1 [1]+ vim 1.txt &
运行一条命令,能够将它丢到后台(前台)去运行 在结束任务的时候,必须是在前台才能结束——>(不然在后台是没法结束任务的)html
[root@localhost ~]# sleep 1000 ^Z [1]+ 已中止 sleep 1000 [root@localhost ~]# jobs [1]+ 已中止 sleep 1000 [root@localhost ~]# sleep 200 ^Z [2]+ 已中止 sleep 200 [root@localhost ~]# jobs [1]- 已中止 sleep 1000 [2]+ 已中止 sleep 200 [root@localhost ~]# fg sleep 200 ^Z [2]+ 已中止 sleep 200
在调到先后台运行的时候,不指定id号,就是默认最后一条执行命令
[root@localhost ~]# sleep 100 & [3] 2239 [root@localhost ~]# jobs [1] 运行中 sleep 100 & [root@localhost ~]#
在打开另外一终端,jobs命令,是查看不到执行当前终端的任务mysql
可是在另外一个终端,能够查看到进程ps aux |grep sleeplinux
``` [root@localhost ~]# ps aux |grep sleep root 2235 0.0 0.0 107892 624 pts/0 T 23:20 0:00 sleep 1000 root 2236 0.0 0.0 107892 620 pts/0 T 23:20 0:00 sleep 200 root 2264 0.0 0.0 112656 984 pts/1 R+ 23:31 0:00 grep --color=auto slee [root@localhost ~]# ```
[root@localhost ~]# a=111 [root@localhost ~]# echo $a 111 [root@localhost ~]# set |grep 111 _=111 a=111 [root@localhost ~]#
[root@localhost ~]# [root@localhost ~]# a1=3 [root@localhost ~]# echo $a1 3 [root@localhost ~]# a_1=2 [root@localhost ~]# echo $a_1 2 [root@localhost ~]# _a1=4 [root@localhost ~]# echo $_a1 4 [root@localhost ~]# 1aa=2 变量名首位不能为数字 -bash: 1aa=2: 未找到命令 [root@localhost ~]#
[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@hf-01 ~]# a="a$bc" //会发现使用双引号,赋值变量得不到想要的结果 [root@hf-01 ~]# echo $a a [root@hf-01 ~]# a='a$bc' //使用单引号会发现正常赋值 [root@hf-01 ~]# echo $a a$bc
[root@hf-01 ~]# a=1 [root@hf-01 ~]# b=2 [root@hf-01 ~]# echo $a$b //变量$a=1,变量$b=2 12 [root@hf-01 ~]# a='a$bc' [root@hf-01 ~]# echo $a$b //变量$a=a$bc,变量$b=2 a$bc2 [root@hf-01 ~]# c="a$bc" [root@hf-01 ~]# echo $c //变量$bc为赋值,因此为空,最后输出的值为a a [root@hf-01 ~]# c="a$b"c [root@hf-01 ~]# echo $c //变量$b=2,,因此输出为a2c a2c
如下例子中,$bc为总体,而我又没有给它赋值,因此为空
当变量或表达式较为复杂的时候,变量叠加的时候,可使用双引号将它们标记起来sql
[root@hf-01 ~]# w 05:34:28 up 4:05, 3 users, load average: 0.00, 0.01, 0.05 USER TTY LOGIN@ IDLE JCPU PCPU WHAT root tty1 01:29 4:04m 0.03s 0.03s -bash root pts/0 01:30 4.00s 0.07s 0.03s w root pts/1 05:34 5.00s 0.02s 0.02s -bash
[root@hf-01 ~]# echo $SSH_TTY //当前是在/dev/pts/0下 /dev/pts/0
- 在终端2下执行命令 echo $SSH_TTY
[root@hf-01 ~]# echo $SSH_TTY /dev/pts/1
[root@hf-01 ~]# hanfeng=linux [root@hf-01 ~]# echo $hanfeng linux
[root@hf-01 ~]# echo $hanfeng //会发现变量为 空 [root@hf-01 ~]#
[root@hf-01 ~]# bash [root@hf-01 ~]#
[root@hf-01 ~]# pstree systemd─┬─NetworkManager───3*[{NetworkManager}] ├─auditd───{auditd} ├─avahi-daemon───avahi-daemon ├─crond ├─dbus-daemon───{dbus-daemon} ├─firewalld───{firewalld} ├─iprdump ├─iprinit ├─iprupdate ├─login───bash ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mysqld_safe───mysqld───20*[{mysqld}] ├─polkitd───5*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd───sshd─┬─bash───bash───pstree │ └─bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd └─tuned───4*[{tuned}] [root@hf-01 ~]#
[root@hf-01 ~]# echo $hanfeng [root@hf-01 ~]#
这是由于这个变量仅仅在上一个shell中
[root@hf-01 ~]# exit exit [root@hf-01 ~]# pstree systemd─┬─NetworkManager───3*[{NetworkManager}] ├─auditd───{auditd} ├─avahi-daemon───avahi-daemon ├─crond ├─dbus-daemon───{dbus-daemon} ├─firewalld───{firewalld} ├─iprdump ├─iprinit ├─iprupdate ├─login───bash ├─lvmetad ├─master─┬─pickup │ └─qmgr ├─mysqld_safe───mysqld───20*[{mysqld}] ├─polkitd───5*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd───sshd─┬─bash───pstree │ └─bash ├─systemd-journal ├─systemd-logind ├─systemd-udevd └─tuned───4*[{tuned}] [root@hf-01 ~]#
这时会看到缺乏了一个bash,由于刚exit退出了一个bash
[root@hf-01 ~]# echo $hanfeng linux
这种定义一个变量叫作非全局,或者叫作本地的变量(仅仅在你终端下生效)shell
[root@hf-01 ~]# export hanfeng=linux [root@hf-01 ~]# echo $hanfeng linux [root@hf-01 ~]# bash [root@hf-01 ~]# echo $hanfeng linux [root@hf-01 ~]#
全局环境变量,在终端1下,在打开shell以后,只要执行export 命令 ,在这下面全部的子shell 都会变量值,但在终端2下,变量依旧是不会生效 全局变量是向下的,在这个shell的基础上生成子shell,子子shell,子子子shell,而不会向上生效
[root@hf-01 ~]# hanfeng=linux [root@hf-01 ~]# echo $hanfeng linux [root@hf-01 ~]# unset hanfeng [root@hf-01 ~]# echo $hanfeng [root@hf-01 ~]#
两个纬度,一个是系统层次,一个是用户层次vim
两种类型,把 bashrc 做为一种类型,把profile做为一种类型centos
~/.bash_logout 文件,用来定义用户退出的时候须要作的一些操做bash
PS1是在/etc/bashrc中定义的ssh
[root@hf-01 ~]# echo $PS1 [\u@\h \W]\$
[root@hf-01 ~]# cd /etc/sysconfig/network-scripts/ [root@hf-01 network-scripts]# PS1='[\u@\h \w]\$' [root@hf-01 /etc/sysconfig/network-scripts]# [root@hf-01 ~]# cd 123/ [root@hf-01 ~/123]# cd /tmp/ [root@hf-01 /tmp]#
这是一个全局路径
[root@hf-01 /tmp]# PS1='\u@\h \w\$' root@hf-01 /tmp#
root@hf-01 /tmp# PS1='<\u@\h \w> \$' <root@hf-01 /tmp> #
普通用户是 $号,root用户是# 号
PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$ '