Linux5.3 Shell变量

查看变量 

  系统变量通常是大写

[root@chy002 ~]# env     #查看系统常用的环境变量
XDG_SESSION_ID=3
HOSTNAME=chy002
... ...

  set 查看系统环境变量和用户自定义的变量,含有很多shell脚本。

[root@chy002 ~]# a=111
[root@chy002 ~]# echo $a
111
[root@chy002 ~]# env |grep 111
[root@chy002 ~]# set |grep 111
a=111

#env就没有,set有

变量命名

  变量名规则:字母、数字下划线,首位不能为数字。

  变量值有特殊符合时需要用单引号括起来。

[root@chy002 ~]# a=1 2 3
-bash: 2: 未找到命令
[root@chy002 ~]# a='1 2 3'
[root@chy002 ~]# echo $a
1 2 3
#有特殊符合还是单引号靠谱
[root@chy002 ~]# b='1$au'
[root@chy002 ~]# echo $b
1$au
[root@chy002 ~]# c="4$ap"
[root@chy002 ~]# echo $c
4
[root@chy002 ~]# c="$a$b"
[root@chy002 ~]# echo $c
12
[root@chy002 ~]# c='`$a``$b`'
[root@chy002 ~]# echo $c
`$a``$b`
[root@localhost ~]# echo $c
4
[root@localhost ~]# a="$cl"
[root@localhost ~]# echo $a

[root@localhost ~]# a="$c"l
[root@localhost ~]# echo $a
4l

全局变量

  export    变量名=xxxxxx

  在任何子shell和其他终端都可以使用。不会向上去全局,子shell的全局父shell不可用。

取消变量

  unset  变量名          取消变量    

环境变量配置文件

系统层次,一般不用修改
  1. /etc/profile 用户环境变量,用户登录自动加载
  2. /etc/bashrc 用户不需要登录,执行shell生效
    PS1在/etc/bashrc里面定义
[root@chy002 ~]# echo $PS1
[u@h W]$
    PS2
  
用户层次,用户家目录下
  1. ~/.bashrc         执行shell生效
  2. ~/.bashrc_profile      用户登录自动加载
  3. ~/.bashrc_history     命令历史
  4. ~/.bashrc_logout    用户退出时做出的操作,如果需要用户退出时删除命令历史,可以把删除命令历史的命令放到logout里
  5. 加载这些配置文件配置   source  文件  或者     .   文件
原文地址:https://www.cnblogs.com/chyuanliu/p/7846468.html