linux-如何在bash -c命令中插入变量
“ bash”命令启动一个子进程,其父进程是您当前的bash会话.
要在父进程中定义变量并在子进程中使用它,必须将其导出.
先看一个简单例子:
$FOO="text"
$echo $FOO
$text
$FOO="text"
$bash -c 'echo $FOO'
$# return nothing
$export FOO="text"
$bash -c 'echo $FOO'
要点:
1、 Subshells will have access to all variables from the parent, regardless of their exported state.
2、 Subprocesses will only see the exported variables.
What is common in these two constructs is that neither can pass variables back to the parent shell.
看下面的例子。
$ noexport=noexport; export export=export; (echo subshell: $noexport $export; subshell=subshell); bash -c 'echo subprocess: $noexport $export; subprocess=subprocess'; echo parent: $subshell $subprocess
subshell: noexport export
subprocess: export
parent:
$ noexport=noexport; export export=export; exec bash -c 'echo execd process: $noexport $export; execd=execd'; echo parent: $execd
execd process: export
上面解释的很清楚了,再来看下面的一个例子和解释:
如果我们创建一个普通变量和一个数组变量,
a=apple # a simple variable
arr=(apple) # an indexed array with a single element
and then echo the expression in the second column, we would get the result / behavior shown in the third column. The fourth column explains the behavior.
| Expression | Result(echo) | Comments
---+-------------+-------------+--------------------------------------------------------------------
1 | "$a" | apple | variables are expanded inside ""
2 | '$a' | $a | variables are not expanded inside ''
3 | "'$a'" | 'apple' | '' has no special meaning inside ""
4 | '"$a"' | "$a" | "" is treated literally inside ''
5 | ''' | invalid | can not escape a ' within ''; use "'" or $''' (ANSI-C quoting)
6 | "red$arocks"| red | $arocks does not expand $a; use ${a}rocks to preserve $a
7 | "redapple$" | redapple$ | $ followed by no variable name evaluates to $
8 | '"' | " | \ has no special meaning inside ''
9 | "'" | ' | ' is interpreted inside "" but has no significance for '
10 | """ | " | " is interpreted inside ""
11 | "*" | * | glob does not work inside "" or ''
12 | "\t\n" | \t\n | \t and \n have no special meaning inside "" or ''; use ANSI-C quoting
13 | "echo hi
" | hi | and $() are evaluated inside "" 14 | '`echo hi`' | `echo hi` |
and $() are not evaluated inside ''
15 | '${arr[0]}' | ${arr[0]} | array access not possible inside ''
16 | "${arr[0]}" | apple | array access works inside ""
17 | $'$a'' | $a' | single quotes can be escaped inside ANSI-C quoting
18 | "$'\t'" | $'\t' | ANSI-C quoting is not interpreted inside ""
19 | '!cmd' | !cmd | history expansion character '!' is ignored inside ''
20 | "!cmd" | cmd args | expands to the most recent command matching "cmd"
21 | $'!cmd' | !cmd | history expansion character '!' is ignored inside ANSI-C quotes
---+-------------+-------------+--------------------------------------------------------------------
原文地址:https://www.cnblogs.com/taowang2016/p/12794073.html
- 在ASP.NET 5中使用SignalR
- 避免在ASP.NET Core中使用服务定位器模式
- 直传文件到Azure Storage的Blob服务中
- 闭路电视探头究竟有多不安全?
- windows API 开发飞机订票系统 图形化界面 (四)
- windows API 开发飞机订票系统 图形化界面 (三)
- windows API 开发飞机订票系统 图形化界面 (二)
- SQL SERVER几种数据迁移/导出导入的实践
- dom4j 使用总结
- 快速对表的某字段赋递增的数值
- SQL SERVER修改函数名引起的问题
- SQL Server安全(1/11):SQL Server安全概述
- SQL Server安全(2/11):身份验证(Authentication)
- SQL Server安全(3/11):主体和安全对象(Principals and Securables)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【Java多线程-4】CompletionService详解
- 【Java多线程-5】 CompletableFuture详解
- 【Java多线程-6】synchronized同步锁
- 【Java多线程-7】阅尽Java千般锁
- 【SpringBoot注解-2】AOP相关注解详解
- 【SpringBoot注解-3】Bean注入相关注解
- 【SpringBoot注解-4】:@Target、@Retention、@Documented注解简介
- 【Linux系列-1】top命令详解
- 【Linux系列-2】iostat命令详解
- 【Mybatis-1】MyBatis注解版详解
- 【MyBatis-2】MyBatis之xml 配置版
- 【MyBatis-3】MyBatis xml映射文件详解
- 【剑指Offer】II-数组中数字出现的次数 II
- 【MyBatis-4】MyBatis之动态SQL
- 【MyBatis-4】MyBatis批量insert、update、delete数据