如何通过golang获取环境变量PS1?

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

How to get environment variable PS1 by golang?

问题

PS1是bash提示符的环境变量。我可以通过echo $PS1来获取它。

我尝试使用os.Getenv来获取PS1,但返回为空:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. fmt.Println(os.Getenv("PS1"))
  8. }

为什么会发生这种情况,我应该如何修复它?
谢谢。

英文:

PS1 is an environment variable for bash prompt. I can get this by echo $PS1

I try to use os.Getenv to get PS1 but returns nothing:

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. fmt.Println(os.Getenv("PS1"))
  8. }

Why does this happen and how should I fix this?
Thanks.

答案1

得分: 6

PS1 可能没有被导出,这意味着它不会在 bash 的子进程中显示。

尝试在运行应用程序之前执行以下命令:

  1. export PS1

你也可以执行以下命令:

  1. PS1=$PS1 app

以在子进程中特定地设置它。

英文:

PS1 is probably not exported, meaning it won't show up in sub-processes of bash

try

  1. export PS1

before you run your app

you could also do

  1. PS1=$PS1 app

to set it specifically in the sub process

huangapple
  • 本文由 发表于 2015年3月31日 16:37:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/29364178.html
匿名

发表评论

匿名网友

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

确定