英文:
os.Getenv and os.LookupEnv don't return the value of $HISTFILE, $HISTSIZE, and $SAVEHIST
问题
我正在使用zsh
,并尝试访问这些环境变量:
$ echo $HISTFILE
/home/amir/.zsh_history
$ echo $HISTSIZE
50000
$ echo $SAVEHIST
10000
但是,os.LookupEnv
对于这三个变量都返回 "", false
,而 os.Getenv
自然地返回空字符串:
package main
import (
"fmt"
"os"
)
func main() {
histfile, found := os.LookupEnv("HISTFILE")
if found {
fmt.Println(histfile)
} else {
fmt.Println("$HISTFILE not found")
}
histsize, found := os.LookupEnv("HISTSIZE")
if found {
fmt.Println(histsize)
} else {
fmt.Println("$HISTSIZE not found")
}
savehist, found := os.LookupEnv("SAVEHIST")
if found {
fmt.Println(savehist)
} else {
fmt.Println("$SAVEHIST not found")
}
}
$ go run main.go
$HISTFILE not found
$HISTSIZE not found
$SAVEHIST not found
现在,如果我在$HOME/.zshrc
中使用export
导出这些变量:
$ grep -iE "histfile|histsize|savehist" $HOME/.zshrc
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE=1000000
export SAVEHIST=1000000
然后它就可以工作了,并且返回了正确的值:
$ go run hyst.go
/home/amir/.zsh_history
1000000
1000000
当我没有显式导出这些变量时,$HISTSIZE
和$SAVEHIST
的值是不同的,但它们不是空的。那么为什么这些函数对于这些变量返回空字符串呢?
英文:
I'm using zsh
, and I'm trying to access these environment variables:
$ echo $HISTFILE
/home/amir/.zsh_history
$ echo $HISTSIZE
50000
$ echo $SAVEHIST
10000
But os.LookupEnv
returns "", false
for all three, and os.Getenv
, naturally, returns an empty string:
package main
import (
"fmt"
"os"
)
func main() {
histfile, found := os.LookupEnv("HISTFILE")
if found {
fmt.Println(histfile)
} else {
fmt.Println("$HISTFILE not found")
}
histsize, found := os.LookupEnv("HISTSIZE")
if found {
fmt.Println(histsize)
} else {
fmt.Println("$HISTSIZE not found")
}
savehist, found := os.LookupEnv("SAVEHIST")
if found {
fmt.Println(savehist)
} else {
fmt.Println("$SAVEHIST not found")
}
}
$ go run main.go
$HISTFILE not found
$HISTSIZE not found
$SAVEHIST not found
Now, if I export
these variables in $HOME/.zshrc
:
$ grep -iE "histfile|histsize|savehist" $HOME/.zshrc
export HISTFILE="$HOME/.zsh_history"
export HISTSIZE=1000000
export SAVEHIST=1000000
Then it works and correct values are returned:
$ go run hyst.go
/home/amir/.zsh_history
1000000
1000000
When I haven't explicitly exported these variables, the values of $HISTSIZE
and $SAVEHIST
are different, but they're not empty. So why do these functions return empty strings for these variables?
答案1
得分: 2
这是因为$HISTFILE
、$HISTSIZE
和$SAVEHIST
默认情况下不是环境变量,而只是由oh-my-zsh
设置的shell变量:
## History file configuration
[ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history"
[ "$HISTSIZE" -lt 50000 ] && HISTSIZE=50000
[ "$SAVEHIST" -lt 10000 ] && SAVEHIST=10000
这些是在使用export
之前在$HOME/.zshrc
中使用echo
时看到的值。
重要的是要区分环境变量和shell变量:
-
环境变量可以在子进程中访问,但是shell变量不能。您可以通过创建一个子进程来验证这个说法:
$ # 子进程不继承shell变量 $ key=value; sh -c 'echo "key=$key"' key= $ # 但是,它们会继承环境变量 $ export key=value; sh -c 'echo "key=$key"' key=value
-
要在子进程中访问shell变量,您可以像在
$HOME/.zshrc
中那样使用export
来导出它:$ key=value; export key; sh -c 'echo "key=$key"' key=value
现在,要查看特定变量是否是环境变量,而不是使用echo
,您可以直接检查env
ironment变量的列表。
$ env | grep -i "shell"; echo $?
SHELL=/usr/bin/zsh
0
所以$SHELL
实际上是一个环境变量。现在来看看历史变量:
$ env | grep -iE "histfile|histsize|savehist"; echo $?
1
退出代码为1,表示在环境变量列表中找不到这些名称。
英文:
That's because $HISTFILE
, $HISTSIZE
, and $SAVEHIST
are not, by default, environment variables, but rather simply shell variables set by oh-my-zsh
:
## History file configuration
[ -z "$HISTFILE" ] && HISTFILE="$HOME/.zsh_history"
[ "$HISTSIZE" -lt 50000 ] && HISTSIZE=50000
[ "$SAVEHIST" -lt 10000 ] && SAVEHIST=10000
These are the values you see when you use echo
before using export
in $HOME/.zshrc
.
It's important to make a distinction between environment and shell variables:
-
Environment variables are accessible in child processes, but shell variables are not. You can verify this statement by simply creating a child process:
$ # Child processes don't inherit shell variables $ key=value; sh -c 'echo "key=$key"' key= $ # They do, however, inherit environment variables $ export key=value; sh -c 'echo "key=$key"' key=value
-
To be able to access a shell variable in the child process, you can
export
it, just as you did in$HOME/.zshrc
:$ key=value; export key; sh -c 'echo "key=$key"' key=value
Now, to see if a specific variable is an environment variable or not, instead of using echo
, you can directly check the list of env
ironment variables.
$ env | grep -i "shell"; echo $?
SHELL=/usr/bin/zsh
0
So $SHELL
is, in fact, an environment variable. Now for the history variables:
$ env | grep -iE "histfile|histsize|savehist"; echo $?
1
The exit code is 1, meaning that it failed to find these names in the list of environment variables.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论