显示系统变量
[root@localhost ~]# env
显示系统环境变量
[root@localhost ~]# set
显示指定变量值
[root@localhost ~]# echo $HOSTNAME
变量定义规则
1、等号前不能有空格
2、变量名必须由大小写字母,数字,下划线组成,不能以数字开头
3、如果变量值中含有特殊字符,必须用单引号。例如:a='cd /home/' b='ls /tmp/'
4、变量值允许调用其他命令的结果,但是必须使用反引号。例如:myvim=`which vim` myhome=`echo $HOME`
5、变量值允许由其他变量叠加其他字符组合组成新的变量。例如:a="$HOME"/test
6、定义全局变量,使用户自定义变量在子shell中继承。例如:export a=1
7、清空变量值
[root@localhost ~]# unset a
自定义登陆系统环境变量,该文件针对所有用户生效,以下path.sh文件名用户可自定义
[root@localhost ~]# vi /etc/profile.d/path.sh
# 文件内容如下:
#!/bin/bash
export PATH=$PATH:/tmp/:/data/bin/
保存配置文件后用命令重新加载,使配置文件即时生效
[root@localhost ~]# source /etc/profile
执行该命令后,系统会自动执行/etc/profile.d/下所有.sh脚本
自定义登陆用户环境变量
[root@localhost ~]# vi ~/.bash_profile 用法同上
自定义注销用户环境变量
[root@localhost ~]# vi ~/..bash_logout 用法同上
自定义用户别名
[root@localhost ~]# vi ~/.bashrc 用法同上
用户历史命令
[root@localhost ~]# vi ~/.bash_history
shell中的特殊符号
* 通配符,多位,如:
[root@localhost ~]# ls *.txt
? 通配符,一位,如:
[root@localhost ~]# ls ?.txt
# 注释行,如:
[root@localhost ~]# #pwd
\ 脱义符(转义符),如:
[root@localhost ~]# touch \#1.txt
| 管道符,如:
[root@localhost ~]# cat install.log | grep samba
& 把命令隐藏(后台)执行,如:
[root@localhost ~]# sleep 1000 &
> 覆盖重定向,如:
[root@localhost ~]# head install.log > 1.txt
>> 追加重定向,如:
[root@localhost ~]# head install.log >> 11.txt
2> 错误覆盖重定向,如:
[root@localhost ~]# ls /etc/*.rar 2>1.txt
2>> 错误追加重定向,如:
[root@localhost ~]# ls /etc/*.rar 2>>11.txt
< 反向重定向,如:
[root@localhost ~]# wc -l < install.log
[] 中括号
[0-9] 表示0-9其中一个数字,如:
[root@localhost ~]# ls [0398].txt
等价于
[root@localhost ~]# ls 0.txt 3.txt 9.txt 8.txt
结果将显示当前目录下0.txt 3.txt 9.txt 8.txt
~ 表示用户家目录,如:
[root@localhost ~]# echo ~
或
[root@localhost ~]# cd ~
; 表示把2条命令写在一行,如:
[root@localhost ~]# ls ~/install ; head ~/install.log
$ 获取变量值,如:
[root@localhost ~]# echo $HOME
$ 表示上一条命令的最后一个参数,如:
[root@localhost ~]# ls /tmp/ | echo !$
/tmp/
$ 表示linux文档行结束符,如:
[root@localhost ~]# cat -A ~/install.log