Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

How to print a Linux environment variable from the Powershell REPL console?

How do I print the value of foo to the console?

nicholas@mordor:~/chimp$ 
nicholas@mordor:~/chimp$ VARNAME="foo"
nicholas@mordor:~/chimp$ export foo="bar" 
nicholas@mordor:~/chimp$ 
nicholas@mordor:~/chimp$ echo $foo
bar
nicholas@mordor:~/chimp$ 
nicholas@mordor:~/chimp$ pwsh
PowerShell 7.1.0
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-Variable HOME -valueOnly
/home/nicholas
PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-Variable foo -valueOnly 
Get-Variable: Cannot find a variable with the name 'foo'.
PS /home/nicholas/chimp> 

see also:

https://askubuntu.com/q/58814/847449 https://opensource.com/article/19/9/environment-variables-powershell

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Certainly foo is listed below:

PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-ChildItem env:

Name                           Value
----                           -----
_                              /snap/bin/pwsh
CLR_ICU_VERSION_OVERRIDE       60.2
COLORTERM                      truecolor
DBUS_SESSION_BUS_ADDRESS       unix:path=/run/user/1000/bus
DESKTOP_SESSION                ubuntu
DISPLAY                        :1
foo                            bar
GDMSESSION                     ubuntu
GJS_DEBUG_OUTPUT               stderr
GJS_DEBUG_TOPICS               JS ERROR;JS LOG
GNOME_DESKTOP_SESSION_ID       this-is-deprecated
GNOME_SHELL_SESSION_MODE       ubuntu
GNOME_TERMINAL_SCREEN          /org/gnome/Terminal/screen/e541ff8a_1fb3_477b_a685_074b316b8bcc
GNOME_TERMINAL_SERVICE         :1.78
GPG_AGENT_INFO                 /run/user/1000/gnupg/S.gpg-agent:0:1
GRADLE_HOME                    /home/nicholas/.sdkman/candidates/gradle/current
GTK_MODULES                    gail:atk-bridge
HOME                           /home/nicholas
IM_CONFIG_PHASE                1
INVOCATION_ID                  8c2492a5c0464f27a979cad923c16937
JAVA_HOME                      /home/nicholas/.sdkman/candidates/java/current
JOURNAL_STREAM                 9:260051
LANG                           en_US.utf8
LANGUAGE                       en_CA:en
LC_ALL                         en_US.UTF-8
LC_CTYPE                       en_US.UTF-8
LESSCLOSE                      /usr/bin/lesspipe %s %s
LESSOPEN                       | /usr/bin/lesspipe %s
LOGNAME                        nicholas
LS_COLORS                      rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:s…
MANAGERPID                     14143
OLDPWD                         /home/nicholas
PATH                           /snap/powershell/149/opt/powershell:/home/nicholas/.sdkman/candidates/java/current/bin:/home/nichol…
POWERSHELL_DISTRIBUTION_CHANN… PSSnap
PSModulePath                   /home/nicholas/.local/share/powershell/Modules:/usr/local/share/powershell/Modules:/snap/powershell…
PWD                            /home/nicholas/chimp
QT_ACCESSIBILITY               1
QT_IM_MODULE                   ibus
SDKMAN_CANDIDATES_API          https://api.sdkman.io/2
SDKMAN_CANDIDATES_DIR          /home/nicholas/.sdkman/candidates
SDKMAN_DIR                     /home/nicholas/.sdkman
SDKMAN_PLATFORM                Linux64
SDKMAN_VERSION                 5.9.1+575
SESSION_MANAGER                local/mordor:@/tmp/.ICE-unix/14354,unix/mordor:/tmp/.ICE-unix/14354
SHELL                          /bin/bash
SHLVL                          1
SNAP                           /snap/powershell/149
SNAP_ARCH                      amd64
SNAP_COMMON                    /var/snap/powershell/common
SNAP_CONTEXT                   q-DbUBQMK4V-sXTO62RcZMXvkKfoVBRGFLw7wD8IiowIRR2JVsv3
SNAP_COOKIE                    q-DbUBQMK4V-sXTO62RcZMXvkKfoVBRGFLw7wD8IiowIRR2JVsv3
SNAP_DATA                      /var/snap/powershell/149
SNAP_INSTANCE_KEY              
SNAP_INSTANCE_NAME             powershell
SNAP_LIBRARY_PATH              /var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void
SNAP_NAME                      powershell
SNAP_REAL_HOME                 /home/nicholas
SNAP_REEXEC                    
SNAP_REVISION                  149
SNAP_USER_COMMON               /home/nicholas/snap/powershell/common
SNAP_USER_DATA                 /home/nicholas/snap/powershell/149
SNAP_VERSION                   7.1.0
SSH_AGENT_PID                  14316
SSH_AUTH_SOCK                  /run/user/1000/keyring/ssh
TERM                           xterm-256color
USER                           nicholas
USERNAME                       nicholas
VTE_VERSION                    6003
WINDOWPATH                     4
XAUTHORITY                     /run/user/1000/gdm/Xauthority
XDG_CONFIG_DIRS                /etc/xdg/xdg-ubuntu:/etc/xdg
XDG_CURRENT_DESKTOP            ubuntu:GNOME
XDG_DATA_DIRS                  /usr/share/ubuntu:/home/nicholas/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:…
XDG_MENU_PREFIX                gnome-
XDG_RUNTIME_DIR                /run/user/1000
XDG_SESSION_CLASS              user
XDG_SESSION_DESKTOP            ubuntu
XDG_SESSION_TYPE               x11
XMODIFIERS                     @im=ibus

PS /home/nicholas/chimp> 

just not sure how to get that specific value:

PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-ChildItem Env:foo           

Name                           Value
----                           -----
foo                            bar

PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-ChildItem Env:foo -valueOnly
Get-ChildItem: A parameter cannot be found that matches parameter name 'valueOnly'.
PS /home/nicholas/chimp> 

not sure why there's no valueOnly:

PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-Variable HOME -valueOnly
/home/nicholas
PS /home/nicholas/chimp> 
PS /home/nicholas/chimp> Get-Variable env:HOME -valueOnly 
Get-Variable: Cannot find a variable with the name 'env:HOME'.
PS /home/nicholas/chimp> 

where HOME has similar behavior.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...