- 浏览: 268607 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
Xujian0000abcd:
说的太好啦~赞一个~
shell if语句中的并列 -
Jimmy.song:
终于在楼主这里找到答案,很受益,谢谢~
使用diff或者vimdiff比较远程文件(夹)与本地文件(夹)
http://www.linuxsir.org/bbs/thread345055.html
shell中的小括号()可以把命令放在子shell中执行,如下:
$var=hello
$(var=hellotest; echo $var)
$hellotest
$echo $var
$hello
说明在子shell中的局部环境变量对父shell不影响
现在运行
$var=hello
$(echo $var) // 子shell只能继承export导出的变量,var=hello是局部变量不会被子shell继承!这里是在子shell中执行的吧?!,下面就不应该显示hello
$hello
$
怎么没有在shell中给var赋值就引用了父shell中的变量了呢?
按照前面的说法:()小括号中的命令在子shell中执行,那么应该是下面才对啊
$var=hello
$(echo $var)
$
$echo $var
$hello
谁解释下,谢谢了
shell确实有很多看似不符合常理的情况,但是manpage里全部交代清楚了!
代码:
Command substitution, commands grouped with parentheses, and asynchro-
nous commands are invoked in a subshell environment that is a duplicate
of the shell environment, except that traps caught by the shell are
reset to the values that the shell inherited from its parent at invoca-
tion. Builtin commands that are invoked as part of a pipeline are also
executed in a subshell environment. Changes made to the subshell envi-
ronment cannot affect the shell's execution environment.shell environment 里到底包括了些什么?往上翻阅manpage!
代码:
The shell has an execution environment, which consists of the follow-
ing:
o open files inherited by the shell at invocation, as modified by
redirections supplied to the exec builtin
o the current working directory as set by cd, pushd, or popd, or
inherited by the shell at invocation
o the file creation mode mask as set by umask or inherited from
the shell's parent
o current traps set by trap
o shell parameters that are set by variable assignment or with set
or inherited from the shell's parent in the environment
o shell functions defined during execution or inherited from the
shell's parent in the environment
o options enabled at invocation (either by default or with com-
mand-line arguments) or by set
o options enabled by shopt
o shell aliases defined with alias
o various process IDs, including those of background jobs, the
value of $$, and the value of $PPID
通过变量赋值的shell参数会成为shell环境的一部分,而通过圆括号创建的子shell所处的环境是父shell环境的一个副本。
也就是说,圆括号创建的子shell,与你直接 bash -c 'do some thing' 创建的子shell环境确实是有些不同的!
(do something) 创建的是子shell;
bash -c 'do something' 创建的也是子shell;
./script.sh 创建的也是子shell;etc。
但是完全复制了父shell环境的就三种情况:
command substitution,命令替换,也就是$(do something);
commands grouped with parentheses,圆括号中的命令组,也就是(do something);
asynchronous commands,异步命令,这个我貌似还没遭遇过... XD
知道$$这个shell参数么?扩展为当前shell的进程号。它是shell环境的一部分,但是不会被子进程继承(怎么可能继承?子进程也是进程,也有自己的进程号!)不过如果子shell中该值没有改变,只有一种解释:子shell复制了父shell的环境。
代码:
$ echo $$
2500
$ (echo $$)
2500
$ echo $(echo $$)
2500
$为什么都是2500?
接着再来:
代码:
$ bash -c 'echo $$'
2530
$ bash
$ echo $$
2531
$ exit
$ echo 'echo $$' | bash
2541
$ cat > script.sh
echo $$
$ bash script.sh
2545
$
shell中的小括号()可以把命令放在子shell中执行,如下:
$var=hello
$(var=hellotest; echo $var)
$hellotest
$echo $var
$hello
说明在子shell中的局部环境变量对父shell不影响
现在运行
$var=hello
$(echo $var) // 子shell只能继承export导出的变量,var=hello是局部变量不会被子shell继承!这里是在子shell中执行的吧?!,下面就不应该显示hello
$hello
$
怎么没有在shell中给var赋值就引用了父shell中的变量了呢?
按照前面的说法:()小括号中的命令在子shell中执行,那么应该是下面才对啊
$var=hello
$(echo $var)
$
$echo $var
$hello
谁解释下,谢谢了
shell确实有很多看似不符合常理的情况,但是manpage里全部交代清楚了!
代码:
Command substitution, commands grouped with parentheses, and asynchro-
nous commands are invoked in a subshell environment that is a duplicate
of the shell environment, except that traps caught by the shell are
reset to the values that the shell inherited from its parent at invoca-
tion. Builtin commands that are invoked as part of a pipeline are also
executed in a subshell environment. Changes made to the subshell envi-
ronment cannot affect the shell's execution environment.shell environment 里到底包括了些什么?往上翻阅manpage!
代码:
The shell has an execution environment, which consists of the follow-
ing:
o open files inherited by the shell at invocation, as modified by
redirections supplied to the exec builtin
o the current working directory as set by cd, pushd, or popd, or
inherited by the shell at invocation
o the file creation mode mask as set by umask or inherited from
the shell's parent
o current traps set by trap
o shell parameters that are set by variable assignment or with set
or inherited from the shell's parent in the environment
o shell functions defined during execution or inherited from the
shell's parent in the environment
o options enabled at invocation (either by default or with com-
mand-line arguments) or by set
o options enabled by shopt
o shell aliases defined with alias
o various process IDs, including those of background jobs, the
value of $$, and the value of $PPID
通过变量赋值的shell参数会成为shell环境的一部分,而通过圆括号创建的子shell所处的环境是父shell环境的一个副本。
也就是说,圆括号创建的子shell,与你直接 bash -c 'do some thing' 创建的子shell环境确实是有些不同的!
(do something) 创建的是子shell;
bash -c 'do something' 创建的也是子shell;
./script.sh 创建的也是子shell;etc。
但是完全复制了父shell环境的就三种情况:
command substitution,命令替换,也就是$(do something);
commands grouped with parentheses,圆括号中的命令组,也就是(do something);
asynchronous commands,异步命令,这个我貌似还没遭遇过... XD
知道$$这个shell参数么?扩展为当前shell的进程号。它是shell环境的一部分,但是不会被子进程继承(怎么可能继承?子进程也是进程,也有自己的进程号!)不过如果子shell中该值没有改变,只有一种解释:子shell复制了父shell的环境。
代码:
$ echo $$
2500
$ (echo $$)
2500
$ echo $(echo $$)
2500
$为什么都是2500?
接着再来:
代码:
$ bash -c 'echo $$'
2530
$ bash
$ echo $$
2531
$ exit
$ echo 'echo $$' | bash
2541
$ cat > script.sh
echo $$
$ bash script.sh
2545
$
发表评论
-
shell 查找文件中包含中文的行
2012-08-15 14:32 2911awk '/[^!-~]/' file asscii码从!到 ... -
Linux Shell for循环写法总结
2012-07-05 15:48 2318关于shell中的for循环用法很多,一直想总结一下,今天网上 ... -
Linux Shell删除两个文件相同部分
2012-07-04 11:22 2295转来的,原址http://www.cnblogs.com/ra ... -
保留字符串中的数字,其它全去掉
2012-03-15 11:26 3516把2007.10.30 16:00:00去掉".&q ... -
sed 用法解释
2011-10-28 14:29 9591.Sed命令 调用sed命令有 ... -
df 和 du 命令详解
2011-09-27 14:59 962df命令详细用法 a:显 ... -
shell tr命令的使用
2011-09-15 10:30 44649tr是translate的简写,亦 ... -
Useful Shell Option – extglob 和 shopt命令
2011-09-14 10:45 3353Bash Shell有个extglob选项 ... -
source命令
2011-09-06 16:45 1231source [filename] 不再产 ... -
Bash 自带的字符截断功能
2011-09-02 15:51 1141如果是一般路径的字符截断可以用basename和dirname ... -
Du命令功能说明
2011-08-24 10:01 957Du命令功能说明:统计 ... -
环境变量
2011-08-22 16:58 775配置环境变量一般需要两个文件 举例: 第一个文件,文件名.p ... -
使用diff或者vimdiff比较远程文件(夹)与本地文件(夹)
2011-08-09 14:53 7967方法1:管道给diff $ssh eric@192.168 ... -
shell里面的特殊字符
2011-08-05 17:15 5072常见的有美元符号($),反斜线(\)和引号。 1。美元符号 ... -
shell 打开文件
2011-08-04 17:43 5710我们都知道shell语句可以执行主程序打开某一文件,比如c:盘 ... -
shell 读取文章行数
2011-08-04 17:42 1066总结了一下有六种方法: 现在有一个a文件,共有55行 ... -
shell uniq
2011-07-22 17:51 1570uniq 命令 文字 uniq是LINUX命令 ... -
shell if语句中的并列
2011-07-21 17:38 29380格式如下,在比较时,数字和字符串用不同的比较符号 1.如果a ... -
shell判断:数值、字符串、文件
2011-07-21 17:34 2252(1)数值测试: -eq 等于则为真。 -ne 不 ... -
shell 有关命令行参数
2011-07-21 17:32 2076通常调用UNIX程序的格式是: command options ...
相关推荐
在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量,命令的用法,如下: 1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:+string},${var:=string},${var:?string} 5.$((exp)) 6.$(var%...
一直很喜欢写unix shell script, 因为写那么一点点东西可以有那么多效果,投入小,产出大,爽啊. 在写IBM AIX K Shell script时,感觉不那么友好,主要是有一些格式细节要求比较严格,不太习惯,后来写惯了也就好了,下面的...
文章目录1、小括号 ——( ):1.1、作用:1.2、关于 ( ) 中使用的运算符:2、双小括号 —— (( )):2.1、作用:2.2、关于 (( )) 中使用的运算符:3、Bash 内部命令 —— [ ] :3.1、作用:3.2、关于 [ ] 中使用的...
/ 554.4 Shell特殊扩展变量的知识与实践 / 60第5章 变量的数值计算实践 / 655.1 算术运算符 / 655.2 双小括号“(())”数值运算命令 / 665.3 let运算命令的用法 / 735.4 expr命令的用法 / 755.5 bc命令的用法 ...
9.7. 双圆括号结构 10. 循环与分支 10.1. 循环 10.2. 嵌套循环 10.3. 循环控制 10.4. 测试与分支(case与select结构) 11. 内部命令与内建命令 11.1. 作业控制命令 12. 外部过滤器, 程序和命令 12.1. 基本命令 12.2. ...
shell的循环主要有3种,for,while,until shell的分支判断主要有2种,if,case 一,for循环 代码如下: #!/bin/bash for file in $(ls /tmp/test...i++)) //注意是双小括号,由于受其他语言的影响,很容易搞错 do e
在Shell中,用括号来表示数组,数组元素用“空格”符号分割开。定义数组的一般形式为: 代码如下:数组名=(值1 值2 … 值n) 例如: 代码如下: array_name=(value0 value1 value2 value3) 或者 代码如下: array_...
Shell 数组用括号来表示,元素用”空格”符号分割开,语法格式如下: array_name=(value1 ... valuen) 实例 #!/bin/bash # author:软件开发网 # url:www.mscto.com my_array=(A B “C” D) 我们也可以使用下标来...
echo是Shell的一个内部指令,用于在屏幕上打印出指定的字符串。命令格式: 代码如下: echo arg 您可以使用echo实现更复杂的输出格式控制。 显示转义字符 代码如下: echo “\”It is a test\”” 结果将是: ...
#!/bin/bash ...以上所述是小编给大家介绍的Linux Shell 判断环境变量 是否存在,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对软件开发网网站的支持!
前言 Bash 内置了很多诸如 ls、cd、mv 这样的重要的命令,也有很多诸如 grep、awk、sed 这些有用的工具。但除此之外,其实 Bash...在 shell 脚本语言中,尖括号可以将数据从一个地方转移到另一个地方。例如可以这样把数
5. 引用(翻译的可能有问题,特指引号) 5.1. 引用变量 5.2. 转义(\) 6. 退出和退出状态 7. Tests 7.1. Test 结构 7.2. 文件测试操作 7.3. 其他比较操作 7.4. 嵌套的if/then 条件test 7.5. 检查你的test 知识 ...
一个小的实用程序,为mkdirp提供括号扩展语法功能。 安装 使用npm进行安装: npm install mkdirps-brackets 用法 熟悉在shell中使用mkdir -p任何人都会熟悉方括号扩展语法。 它允许一次创建多个子目录。 例如,...
打印所有通过网关snup的ftp数据包(注意, 表达式被单引号括起来了, 这可以防止shell对其中的括号进行错误解析) 代码如下:tcpdump ‘gateway snup and (port ftp or ftp-data)’ 打印所有源地址或目标地址是本地主机的...
双圆括号结构 10. 循环与分支 10.1. 循环 10.2. 嵌套循环 10.3. 循环控制 10.4. 测试与分支(case与select结构) 11. 内部命令与内建命令 11.1. 作业控制命令 12. 外部过滤器, 程序和命令 12.1. 基本...
双圆括号结构 10. 循环和分支 10.1. 循环 10.2. 嵌套循环 10.3. 循环控制 10.4. 测试与分支(case和select结构) 11. 内部命令与内建 11.1. 作业控制命令 12. 外部过滤器,程序和命令 12.1. 基本命令 ...
排序算法(平方排序算法的应用,Shell排序,快速排序,归并排序,时间复杂度下界,三种线性时间排 序,外部排序) 数论(整除,集合论,关系,素数,进位制,辗转相除,扩展的辗转相除,同余运算,解线性同余...
[] 匹配中括号里的内容[a-z][A-Z][0-9]。 ! 事件。 $ 取环境变量的值。 | 管道。把前一命令的输出作为后一命令的输入,把几个命令连接起来。 |经常跟tee连用,tee 把内容保存到文档并显示出来。 三、通用后...
9.7. 双圆括号结构 10. 循环和分支 10.1. 循环 10.2. 嵌套循环 10.3. 循环控制 10.4. 测试与分支(case 和select 结构) 11. 内部命令与内建 11.1. 作业控制命令 12. 外部过滤器,程序和命令 12.1. 基本命令 12.2. 复杂...