如何读取zsh shell变量

huangapple go评论101阅读模式
英文:

How to read the zsh shell variables

问题

我正在运行我的Go程序,从zsh中运行。该程序需要读取$fpath变量。但是尝试在Os.Env中读取fpath,但它返回空字符串(即变量似乎不存在)。

我不确定为什么会发生这种情况,因为该变量已经在shell中可用。对此可能的解释是什么?

此外,有没有解决方案可以在Go程序中实现上述功能?

英文:

I am running my go program, from zsh. The program needs to read $fpath variables. But trying to read fpath on Os.Env, but it is returning empty string(ie. like the variable does not exist).

I am not sure why this should be happening since the variable is already available in the shell. What could be an explanation for this?

Also, any solutions to how can the above be accomplished within the go program?

答案1

得分: 3

你需要将zsh变量的值导出,以便通过环境变量或命令行参数使其对外部进程(如你的Go程序)可用。

在下面的命令中,你可以将/usr/bin/env替换为你自己的程序。

my_fpath="$fpath" env
my_fpath="$fpath" env | grep my_fpath
英文:

You need to export the values of the zsh variables to make it available to an external process such as your Go program via environment variables or command-line arguments.

In the following commands you can replace /usr/bin/env by your own program.

my_fpath="$fpath" env
my_fpath="$fpath" env | grep my_fpath

答案2

得分: 2

Go,或者其他任何应用程序,都无法访问zsh变量。我认为你对环境变量感到困惑了。

在你的shell中运行printenv命令,你会注意到它也不会列出你的fpath变量。

如果你想让这个变量在应用程序中可用,可以使用shell扩展,在执行应用程序时将其放入环境中;
FPATH="$fpath" printenv

或者将其作为参数传递,并使用flags包来读取它;
./app -fpath "$fpath"

英文:

Go, or any other application for that matter, can't access zsh variables. I think you are confused with environmental variables.
Run printenv in your shell and notice it won't list your fpath variable either.

If you want this variable to be available to an application, use shell expansion to put it in the environment when executing an application;
FPATH="$fpath" printenv

Or pass it as an argument and read it using the flags-package;
./app -fpath "$fpath"

huangapple
  • 本文由 发表于 2021年7月30日 13:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/68585699.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定