关于shell变量的继承总结
结论:
默认,父shell和子shell的变量是隔离的。
sh方式运行脚本,会重新开启一个子shell,无法继承父进程的普通变量,能继承父进程export的全局变量。
source或者. 方式运行脚本,会在当前shell下运行脚本,相当于把脚本内容加载到当前shell后执行,自然能使用前面定义的变量。
验证:在子shell中调用父shell普通变量
[root@gjt scripts]# echo $b [root@gjt scripts]# echo $a [root@gjt scripts]# b=gaga
[root@gjt scripts]# echo $b
gaga
[root@gjt scripts]# cat test1.sh
a=haha
echo "test1: $a"
echo "test1: $b"
sh /root/scripts/test2.sh
[root@gjt scripts]# cat test2.sh
echo "test2:$a"
echo "test2:$b"
[root@gjt scripts]# sh test1.sh
test1: haha
test1:
test2:
test2:
#执行过程解释:
sh test1.sh ==>重新启动一个子shell
a=haha ==>a变量赋值
echo "test1: $a" ==>输出:test1: haha
echo "test1: $b" ==>输出:test1: 因为子shell不会继承父shell的普通变量,所以$b为空
sh /root/scripts/test2.sh ==>重新启动一个子shell
echo "test2:$a" ==>输出:test2: 同上,$a为空
echo "test2:$b" ==>输出:test2: 同上,$b为空 [root@gjt scripts]# source test1.sh
test1: haha
test1: gaga
test2:
test2:
[root@gjt scripts]# echo $a
haha
#执行过程解释:
source test1.sh ==>在当前shell下执行脚本
a=haha ==>a变量赋值
echo "test1: $a" ==>输出:test1: haha
echo "test1: $b" ==>输出:test1: gaga 在运行脚本之前在终端定义了b变量。
sh /root/scripts/test2.sh ==>重新启动一个子shell
echo "test2:$a" ==>输出:test2: $a未定义
echo "test2:$b" ==>输出:test2: $b未定义 [root@gjt scripts]# echo $a ==>输出:haha,source test1.sh时定义了。
验证:在子shell中调用父shell普通变量
验证:在子shell中调用父shell定义的export全局变量
[root@gjt scripts]# echo $b [root@gjt scripts]# echo $a [root@gjt scripts]# cat test1.sh
export a=haha
echo "test1: $a"
echo "test1: $b"
sh /root/scripts/test2.sh
[root@gjt scripts]# cat test2.sh
echo "test2:$a"
echo "test2:$b"
[root@gjt scripts]# export b=gaga
[root@gjt scripts]# sh test1.sh
test1: haha
test1: gaga
test2:haha
test2:gaga #输出说明,父shell定义的全局变量可以传递给子shell以及子shell的子shell
验证:在子shell中调用父shell定义的export全局变量
[root@gjt scripts]# echo $b [root@gjt scripts]# echo $a [root@gjt scripts]# cat test1.sh
export a=haha
echo "test1: $a"
echo "test1: $b"
sh /root/scripts/test2.sh
[root@gjt scripts]# cat test2.sh
echo "test2:$a"
echo "test2:$b"
[root@gjt scripts]# export b=gaga
[root@gjt scripts]# sh test1.sh
test1: haha
test1: gaga
test2:haha
test2:gaga
[root@gjt scripts]# echo $a [root@gjt scripts]# #最后的$a输出为空,说明子shell运行结束后,其定义的全局变量和普通变量均自动销毁。
验证:在父shell中无法调用子shell定义的export全局变量
注意:测试过程中如果使用了source运行脚本,请退出终端或unset再进行其他测试,避免变量的值对其他验证有影响。
关于shell变量的继承总结的更多相关文章
- (转载)linux中shell变量
(转载)http://blog.csdn.net/zahuopuboss/article/details/8633891 为使shell编程更有效,系统提供了一些shell变量.shell变量可以保存 ...
- 【shell编程基础1】shell变量篇
Bash shell bash shell 是bourne shell 的升级版,“bourne again shell”.ubuntu的默认shell. 预备知识 1. "#!" ...
- Shell脚本编程(二):shell变量
定义变量 定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程 ...
- linux 中的局部变量、全局变量、shell 变量的总结
系统局部变量和全局变量 一.变量分类局部变量和环境变量,局部变量只适用于当前shell,而环境变量是全局的,它适用于所有当前shell以及其派生出来的任意子进程,有些变量是用户创建的,其他的则是专用 ...
- shell变量与运算
shell变量与运算 @(0003 shell编程) 变量存在于内存中.假设变量str,设置或修改变量属性时,不带$号,只有引用变量的值时才使用$号.也就是说在内存中,标记变量的变量名称是str,而不 ...
- 二、Shell变量
类型 注释强变量 变量在使用前,必须事先声明,甚至还需要初始化 弱变量 变量用时声明,甚至不区分类型 变量的作用:用来保存变化的数据 变量名 名称固定,由系统设定或用户定义 变量值 根据用户设 ...
- Shell变量的作用域:Shell全局变量、环境变量和局部变量
Shell 变量的作用域(Scope),就是 Shell 变量的有效范围(可以使用的范围). 在不同的作用域中,同名的变量不会相互干涉,就好像 A 班有个叫小明的同学,B 班也有个叫小明的同学,虽然他 ...
- shell变量
定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
随机推荐
- eplan图框制作
1. 首先,新建一个原理图项目 2. 新建图框.选择“工具”→“主数据”→“图框”→“新建” 在“文件名”中输入文件名,保存. 3.添加新建图框属性选项.选中“新建符号*”,添加选项 4. 设置图框的 ...
- 如何用Eclipse创建一个JavaSwing的项目
创建之前必须先给开发工具安装WindowBuilder插件(安装方法可自行百度) 方式一: 创建项目 new--other--WindowBuilder--SWT Designer----SWT/JF ...
- AppiumDesktop录制脚本
AppiumDesktop启动页面: 启动AppiumDesktop以后点击该页面右上角的Start New Session按钮,就会启动一个新的会话窗口(如下图),在这个窗口我们需要配置一些Desi ...
- 初始ajax技术
一.AJAX是啥? 1.页面无需刷新,异步请求. 2.为什么使用ajax? 原因: 1传统模式 需要将请求发送到服务器,服务器经过业务处理,返回一个页面给客户端.这样做,会很浪费资源. 2.ajax ...
- [CF 1043F] Make It One
Description 给定 \(n\) 个正整数 \(a_i\),最少选出多少个 \(a_i\) 使得他们 \(gcd\) 为 \(1\)?\(n,a_i\le 3\times 10^5\). So ...
- jQuery中$.each()方法(遍历)
$.each()是对数组,json和dom结构等的遍历,说一下他的使用方法吧. 1.遍历一维数组 var arr1=['aa','bb','cc','dd']; $.each(arr1,functio ...
- java 数字左补齐0
NumberFormat nf = NumberFormat.getInstance(); //设置是否使用分组 nf.setGroupingUsed(false); ...
- JAVA helloworld!
idea创建java项目 https://jingyan.baidu.com/article/48b558e3f8f6637f39c09a44.html 本地文档运行 java helloworld ...
- DML、DDL、DCL的分别是什么
DML.DDL.DCL的分别是什么 一直以来,分不清这三者的简称代表什么,甚至在面试中遇到可能会张冠李戴.今天特意记录一下. 一.DML(data manipulation language) 数据操 ...
- codeforces-1142 (div1)
div1真难,现在就是后悔, 非常后悔 A.显然如果我们知道起点和终点是哪两个点,我们可以算出距离通过b / gcd(a,b)的方式求出需要走几步的. 并且数据范围似乎也允许我们这么做,所以直接枚举取 ...