在调用 shell 中设置环境变量的最佳方法 [英] Best way to set environment variables in calling shell

查看:28
本文介绍了在调用 shell 中设置环境变量的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要能够设置和使用上下文的实用程序.我希望它在 shell 中使用环境变量,以便它可以记住它当前在调用之间的上下文.理想情况下,我想从实用程序本身设置这个环境变量.比如:

I'm writing a utility that needs to be able to set and use context. I'd like it to use environment variables in the shell so that it can remember what context it's currently in between invocations. Ideally, I'd like to set this environment variable from within the utility itself. Something like:

mytool set-context <context>
mytool do-stuff # Aware of <context>

我希望这样的行为:

export MYTOOL_CONTEXT=<context>
mytool do-stuff # Aware of <context>

现在,程序实际上不可能在调用 shell 的环境中设置环境变量(在 SO 这里).然而,这正是我想要的那种行为,我正在寻找一种近似或解决方法.

Now, it's not actually possible for a program to set environment variables in the environment of the calling shell (covered on SO here). However, that's the sort of behavior I want, and I'm looking for an approximation or workaround.

我的一些想法:

  • 输出正确的字符串,并期望用户设置变量(假设变量不是简单地设置为 <context>,而是设置为从它派生的某个字符串).这看起来像 export MYTOOL_CONTEXT=$(mytool get-context-var <context>).
  • 输出完整的设置命令.这看起来像 $(mytool set-context <context>),其中实际执行了命令的输出(并且输出类似于 export MYTOOL_CONTEXT=<context>).
  • 保存到一个临时文件,其名称基于 shell 的 PID.这在大多数情况下都有效,但不会自动删除文件,因此它可能会一直存在直到重新启动(即,在许多机器上可能永远存在).
  • Output the proper string, and expect the user to set the variable (assuming that the variable isn't simply set to <context>, but instead to some string derived from it). That would look something like export MYTOOL_CONTEXT=$(mytool get-context-var <context>).
  • Output the full command for setting it. That would look something like $(mytool set-context <context>), where the output of the command is actually executed (and the output would be something like export MYTOOL_CONTEXT=<context>).
  • Save to a temporary file whose name is based on the PID of the shell. This would work in most cases, but removing the file wouldn't happen automatically, so it would probably just lie around until reboot (ie, possibly forever on many machines).

还有其他想法吗?谢谢!

Any other ideas? Thanks!

注意:以上示例使用 BASH 语法,但在大多数 shell 中都有等价物.

Note: The above examples use BASH syntax, but there are equivalents in most shells.

推荐答案

澄清前

POSIX shell(bashksh 等)和 Bourne shell 允许您在命令行上仅为一个命令设置环境变量:

Before clarification

The POSIX shells (bash, ksh, etc) and the Bourne shell allow you to set environment variables on a command line for one command only:

MYTOOL_CONTEXT=<context> mytool do-stuff

这会将 MYTOOL_CONTEXT 设置为 mytool 调用的环境变量(仅).顺便说一句,大多数 shell 接受一个选项 -k(POSIX 没有标准化),这意味着所有看起来像 VAR=value 的参数都被视为环境变量,即使它们出现在命令名称之后.这是一个实用价值相当有限的好奇心,这就是它没有标准化的原因.

This sets MYTOOL_CONTEXT as an environment variable for that invocation of mytool (only). Incidentally, most shells accept an option -k (which is not standardized by POSIX) that means all arguments that look like VAR=value are treated as environment variables, even when they appear after the command name. This is a curiosity with rather limited practical value, which is why it is not standardized.

POSIX env 命令是旨在允许您控制调用命令的环境,尽管它通常不带任何参数来列出当前环境.因此,或者,您可以使用:

The POSIX env command is designed to allow you to control the environment of an invoked command, though it is commonly used without any arguments to list the current environment. So, alternatively, you might use:

env MYTOOL_CONTEXT=<context> mytool do-stuff

env 的优点是您可以在设置命令行中指定的环境变量之前取消设置每个环境变量(这样您就可以完全控制环境).

The advantage of env is that you can do things like unset every environment variable before setting the ones specified in the command line (so you get total control over the environment).

如果打算设置环境以供后续使用,那么 . 命令就是要使用的命令.您可以创建一个文件 context,其中包含要执行的命令以设置环境.然后你可以使用:

If the intention is to set the environment for subsequent use, then the . command is the one to use. You can create a file, context, which contains commands to be executed to set the environment. Then you can use:

. context

并且文件的内容会在当前shell的上下文中执行,所以你可以设置环境变量等.Bash为提供了一个同义词source.命令.它的灵感来自不提供 . 命令但提供 source 作为等效命令的 C shell.. 命令的参数命名的文件将在 $PATH 上搜索,但不需要可执行(可读即可).如果您提供额外的参数,它们将成为 . 命令期间的位置参数($1 等).还要注意,点文件创建的任何变量或函数在文件完成时仍然有效(与普通脚本中的变量不同,在脚本完成时会消失).如果您不打算污染用户的名称空间,则必须小心.

and the contents of the file will executed in the context of the current shell, so you can set environment variables, etc. Bash provides a synonym, source, for the . command. It was inspired by the C shell which does not provide the . command but does provide source as an equivalent. The file named as an argument to the . command will be searched for on $PATH but does not need to be executable (readable is sufficient). If you provide extra arguments, they become the positional parameters ($1, etc) for the duration of the . command. Note too that any variables or functions created by the dotted file remain in effect when the file is finished (unlike variables in a normal script which vanish when the script completes). If you aren't going to pollute the user's name space, you have to be careful.

这篇关于在调用 shell 中设置环境变量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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