shell环境变量说明

查看设置环境变量值

查看所有环境变量

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脚本中重新定义!

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值