内联变量分配和Bash中常规变量的区别是什么? [英] What is the difference between an inline variable assignment and a regular one in Bash?

查看:57
本文介绍了内联变量分配和Bash中常规变量的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

prompt$   TSAN_OPTIONS="suppressions=/somewhere/file" ./myprogram

prompt$   TSAN_OPTIONS="suppressions=/somewhere/file"
prompt$   ./myprogram

线程清理器库提供了第一种情况获取其库(在myprogram中使用)以读取options中给出的文件.我读了它,并假设它应该是两行,所以将其作为第二种情况来运行.

The thread-sanitizer library gives the first case as how to get their library (used within myprogram) to read the file given in options. I read it, and assumed it was supposed to be two separate lines, so ran it as the second case.

在第二种情况下,库不使用文件,因为环境变量和程序执行位于单独的行上.

The library doesn't use the file in the second case, where the environment variable and the program execution are on separate lines.

有什么区别?

奖金问题:第一种情况如何甚至没有错误运行?应该不应该有一个;或&&它们之间?这个问题的答案可能会回答我的第一个...

Bonus question: How does the first case even run without error? Shouldn't there have to be a ; or && between them? The answer to this question likely answers my first...

推荐答案

格式 VAR = value命令将变量 VAR 设置为具有值 value 在命令 command 的环境中.涵盖此内容的规范部分是简单命令.具体来说:

The format VAR=value command sets the variable VAR to have the value value in the environment of the command command. The spec section covering this is the Simple Commands. Specifically:

否则,变量分配应导出到命令的执行环境,并且不会影响当前的执行环境,除非是在步骤4中执行扩展的副作用.

Otherwise, the variable assignments shall be exported for the execution environment of the command and shall not affect the current execution environment except as a side-effect of the expansions performed in step 4.

格式为 VAR = value;command 当前shell 中设置shell变量 VAR ,然后将 command 作为子进程运行.子进程对shell进程中设置的变量一无所知.

The format VAR=value; command sets the shell variable VAR in the current shell and then runs command as a child process. The child process doesn't know anything about the variables set in the shell process.

进程导出(提示)子进程要查看的变量的机制是通过在运行子进程之前在其环境中设置变量.内置的shell是 export .这就是为什么您经常看到 export VAR = value VAR = value;的原因.导出VAR .

The mechanism by which a process exports (hint hint) a variable to be seen by child processes is by setting them in its environment before running the child process. The shell built-in which does this is export. This is why you often see export VAR=value and VAR=value; export VAR.

您正在讨论的语法是类似于以下内容的缩写形式:

The syntax you are discussing is a short-form for something akin to:

VAR=value
export VAR
command
unset -v VAR

仅完全不使用当前的处理环境.

only without using the current process environment at all.

这篇关于内联变量分配和Bash中常规变量的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆