在程序执行的同一行设置环境变量与单独设置环境变量不同吗? -Shell变量与环境变量 [英] Setting an environment variable on same line as program execution is different than setting it separately? - shell variables vs. environment variables

查看:238
本文介绍了在程序执行的同一行设置环境变量与单独设置环境变量不同吗? -Shell变量与环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两者之间有什么区别

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 command在命令command的环境中将变量VAR设置为具有值value.涵盖此内容的规范部分是简单命令.具体来说:

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=valueVAR=value; export 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.

这篇关于在程序执行的同一行设置环境变量与单独设置环境变量不同吗? -Shell变量与环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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