shell export path_详解shell脚本的环境、普通、特殊变量

#变量介绍
变量就是用一个固定的字代替复杂的内容,该内容里可能还会包含变量、路径、字符串等其它的内容。

#变量类型

分为环境变量(全局变量)和普通变量(局部变量)环境变量也可称全局变量普通变量又称局部变量
一、环境变量

环境变量一般用export内置命令导出变量,用于定义shell的运行环境,所有的环境变量都是系统全局变量
#提示1:定义的变量均为大写,环境变量应用于用户进程前,使用export命令导出
#提示2:变量赋值两边应没有空格
#示例:export DIR=/data/

#设置环境变量

#在用户家目录或全局配置中进行设置.bashrc/etc/bashrc/etc/profile

#显示与取消环境变量

#1.通过echo或print命令打印环境变量$HOME : 用户登录时进入的目录$UID :当前用户的UID,相当于id-u$PWD :当前工作目录的绝对路径$SHELL :当前SHELL$USER : 当前用户#示例[root@game ~]# echo $HOME/root[root@game ~]# echo $UID0[root@game ~]# echo $PWD/root[root@game ~]# echo $SHELL/bin/bash[root@game ~]# echo $USERroot#2.通过env或set显示默认的环境变量#3.通过unset消除本地变量和环境变量[root@game ~]# echo $PWD/root[root@game ~]# unset PWD[root@game ~]# echo $PWD[root@game ~]#

#自定义环境变量

命令:export或declare内置命令#格式export 变量名=valuedeclare -x 变量名 value#示例[root@game ~]# export DIRR=guoke[root@game ~]# declare -x DIRR=guoke

#自定义全局变量示例

[root@game ~]# cat /etc/profile |grep guokeexport DIRR='guoke'[root@game ~]# source /etc/profile[root@game ~]# echo $DIRRguoke[root@game ~]# env |grep DIRRDIRR=guoke
二、普通变量

本地变量一般在脚本或命令中进行定义,只在当前的shell中有效

#变量名一般是由字母、数字、下划线组成,可以以字母或下划线开头

#普通变量定义

变量名=value #不加引号变量名='value' #加单引号变量名="value" #加双引号#示例[root@game test]# cat test.shmail=guoke@qq.com
三、特殊变量

#常用的特殊变量

特殊变量	解释$0	获取当前执行的脚本文件名,如果脚本包含路径,则打印脚本路径$n	获取第n个执行脚本参入的值,当n大于9,使用{}阔起来$#	获取脚本传参数得总个数$*	获取脚本所有传参的参数$@	获取脚本所有传参的参数$?	获取上一个命令执行的返回值,0为成功,非0为失败$$	获取脚本的进程号PID$!	获取上一次执行脚本的pid$_	获取上一条命令的最后一个参数值#获取更多帮助使用man bash,搜索关键字:"Special Parameters"

ac2938e4d93eca7ba3df33455a494253.png

#详解1.$0:获取执行脚本的名字,当n大于9,使用大括号{}括起来,防止输出不正确
#示例

#获取脚本的名称路径[root@game test]# cat test.sh #!/bin/bashecho $0#执行效果[root@game test]# sh test.sh test.sh#带路径就显示路径[root@game test]# sh /server/scripts/test/test.sh /server/scripts/test/test.sh

#扩展:dirname,basename
dirname:获取脚本的路径
basename:获取脚本的名字

#简单示例[root@game test]# dirname /server/scripts/test/test.sh /server/scripts/test[root@game test]# basename /server/scripts/test/test.sh test.sh

#企业应用

[root@game test]# cat test5.sh #!/bin/bashMY_PATH=$(cd `dirname $0`; pwd)echo ${MY_PATH}#执行效果[root@game test]# sh test5.sh /server/scripts/test

2.$n:执行脚本传入的参数值
#简单示例

#接收一个传入的参数[root@game test]# cat test.sh #!/bin/bashecho $1 #打印脚本传递的第一个参数的值#执行效果[root@game test]# sh test.sh guokeguoke#接收2个参数[root@game test]# cat test.sh #!/bin/bashecho $1 $2[root@game test]# sh test.sh guoke guoke1guoke guoke1#当需要传入的参数大于9,使用大括号括起来[root@game test]# cat test.sh #!/bin/bashecho $1 $2 $3 $4 $5 $6 $7 $8 $9 ${10} ${11}

#企业场景定义的传参方式

[root@game test]# cat test6.sh  #执行脚本需要接3个参数#!/bin/bashusage(){   echo "usage:$0 key start endexample$0 ff 1 2"}

3.$#:获取传参的总个数
#简单示例

[root@game test]# cat test.sh #!/bin/bashecho $1 $2 $3 $4 $5 $6echo $##执行效果[root@game test]# sh test.sh  1 2 3 4 5 61 2 3 4 5 66

#企业场景应用

[root@game test]# cat test6.sh #!/bin/bashusage(){   echo "usage:$0 key start endexample$0 ff 1 2"}[ $# -ne 3 ] && usage && exit 1 #判断传入的参数如果不等于3,那么就打印帮助函数,退出脚本

4.$*:获取当前脚本所有传参的参数,不加引号和$@相同

#简单示例

#通过set设置3个参数进行打印[root@game ~]# set wo me guoke[root@game ~]# echo $@wo me guoke[root@game ~]# echo $*wo me guoke

5.$?:获取上一个指令的返回值

#说明:$?一般是用来判断命令或脚本是否执行成功,返回值为0就说明执行成功,否则就是失败#简单示例:判断命令是否执行成功[root@game ~]# cd /server/scripts/test/[root@game test]# echo $?0 #命令执行成功就会返回0[root@game test]# cd /server/scripts/guoke-bash: cd: /server/scripts/guoke: No such file or directory[root@game test]# echo $?1 #如果命令执行失败就会返回非0

6.$$:获取当前执行的脚本的进程号
7.$!:获取上一次执行脚本的pid

63bdf7170d62ed77be61edc582649bf6.png

扫描二维码,关注老油条IT记

                                                                    欢迎留言

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值