查看设置环境变量值
查看所有环境变量
set:输出所有环境变量,包括全局环境变量和局部环境变量;
env:只显示全局环境变量;
declare:输出所有变量、函数、整数和已经导出的变量;
set -o :显示bash shell的所有参数配置信息;
查看某个环境变量
[root@hostname ~]# echo $HOME
/root
[root@hostname ~]# echo $UID
0
[root@hostname ~]# echo $PWD
/root
[root@hostname ~]# echo $SHELL
/bin/bash
[root@hostname ~]# echo $USER
root
[root@hostname ~]# printf "$UID \n"
0
取消环境变量
unset 变量名
自定义环境变量
设置临时环境变量
export name=value
name=value ; export name
declare -x name=value
配置用户环境变量(某个用户)
配置该用户家目录下的文件:
/root/.bashrc
/root/.bash_profile
/home/username/.bashrc
/home/username/.bash_profile
配置所有用户生效的环境变量(全局)
/etc/profile
/etc/bashrc
/etc/profile.d/xx.sh
设置登录提示
一、在/etc/motd增加提示的字符
[xxx@hostname ~]$ cat /etc/motd
hi boy,let's go!
Welcome to Alibaba Cloud Elastic Compute Service !
二、在/etc/profile.d下面增加脚本
[root@hostname ~]# cat /etc/profile.d/test.sh
echo "hello,welcome to centos7.4"
登录后提示如下:
[xxx@hostname ~]$ go 192.168.2.93
Last login: Sat May 18 00:40:33 2019 from 10.26.48.30
hi boy,let's go! //第一句
Welcome to Alibaba Cloud Elastic Compute Service !
hello,welcome to centos7.4 //第二句
环境变量总结
- 环境变量名称需要大些;
- 通常使用export定义环境变量;
- env 默认可以显示所有全局变量的名称和值;
- 使用变量$name 取消环境变量 unset name;
- 书写从容的定时任务时需要注意:脚本中需要用到环境变量,最好先在所执行的shell脚本中重新定义!