如果 var 名称很长且有空格,如何在 shell 中引用和查看 env var?如何使用回声呢?
how to refer and see env var in shell if the var name is long and has spaces? how to use echo on it?
我的环境变量是 IntelliJ IDEA 社区版。使用 win10 + gitbash.
IntelliJ IDEA Community Edition C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin
回显怎么做?这不起作用:
>echo $IntelliJ IDEA Community Edition
>echo ($IntelliJ IDEA Community Edition)
>echo $%IntelliJ IDEA Community Edition%
>echo %IntelliJ IDEA Community Edition%
>echo '$%IntelliJ IDEA Community Edition%'
>echo 'IntelliJ IDEA Community Edition'
其他没有空格的环境变量,如 QT_SCALE_FACTOR_ROUNDING_POLICY 可以工作
使用
echo $QT_SCALE_FACTOR_ROUNDING_POLICY
在 Charles Duffy 的帮助下编辑:
我用过
环境 | grep IDEA,我确实得到了 'IDEA' 的所有东西,包括我需要的路径:
$ env | grep IDEA
IntelliJ IDEA Community Edition=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin;
PATH=/c/Users/erjan/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/erjan/bin:/c/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files/PuTTY:/c/Program Files/Calibre2:/c/Program Files/nodejs:/cmd:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/python38/Scripts:/c/python38:/c/Users/erjan/AppData/Local/Microsoft/WindowsApps:/c/Program Files/Azure Data Studio/bin:/c/Users/erjan/AppData/Local/Programs/Microsoft VS Code/bin:/c/Users/erjan/AppData/Roaming/npm:/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2020.3.2/bin:/c/kafka_2.12-2.8.0/bin/windows:/usr/bin/vendor_perl:/usr/bin/core_perl
ORIGINAL_PATH=/mingw64/bin:/usr/bin:/c/Users/erjan/bin:/c/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files/PuTTY:/c/Program Files/Calibre2:/c/Program Files/nodejs:/cmd:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/python38/Scripts:/c/python38:/c/Users/erjan/AppData/Local/Microsoft/WindowsApps:/c/Program Files/Azure Data Studio/bin:/c/Users/erjan/AppData/Local/Programs/Microsoft VS Code/bin:/c/Users/erjan/AppData/Roaming/npm:/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2020.3.2/bin:/c/kafka_2.12-2.8.0/bin/windows
由于您在 Windows 上并且没有 procfs,检索完整环境的最简单方法是使用 env
。
请注意,shell 是否会从环境中删除名称与合法 shell 变量名称不匹配的环境变量(因此也使它们对 env
等工具不可用)是实现定义的; POSIX sh 标准使得 shells 删除这些名称是合法的,但保留它们也是合法的,所以这是否有效可能因版本而异。
idea=$(env | sed -rne '/^IntelliJ IDEA Community Edition=/ { s/^[^=]+=//p }')
if [[ $idea ]]; then
echo "Found IntelliJ at: $idea"
else
echo "Could not find IntelliJ"
fi
我的环境变量是 IntelliJ IDEA 社区版。使用 win10 + gitbash.
IntelliJ IDEA Community Edition C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin
回显怎么做?这不起作用:
>echo $IntelliJ IDEA Community Edition
>echo ($IntelliJ IDEA Community Edition)
>echo $%IntelliJ IDEA Community Edition%
>echo %IntelliJ IDEA Community Edition%
>echo '$%IntelliJ IDEA Community Edition%'
>echo 'IntelliJ IDEA Community Edition'
其他没有空格的环境变量,如 QT_SCALE_FACTOR_ROUNDING_POLICY 可以工作 使用
echo $QT_SCALE_FACTOR_ROUNDING_POLICY
在 Charles Duffy 的帮助下编辑:
我用过 环境 | grep IDEA,我确实得到了 'IDEA' 的所有东西,包括我需要的路径:
$ env | grep IDEA
IntelliJ IDEA Community Edition=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin;
PATH=/c/Users/erjan/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/erjan/bin:/c/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files/PuTTY:/c/Program Files/Calibre2:/c/Program Files/nodejs:/cmd:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/python38/Scripts:/c/python38:/c/Users/erjan/AppData/Local/Microsoft/WindowsApps:/c/Program Files/Azure Data Studio/bin:/c/Users/erjan/AppData/Local/Programs/Microsoft VS Code/bin:/c/Users/erjan/AppData/Roaming/npm:/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2020.3.2/bin:/c/kafka_2.12-2.8.0/bin/windows:/usr/bin/vendor_perl:/usr/bin/core_perl
ORIGINAL_PATH=/mingw64/bin:/usr/bin:/c/Users/erjan/bin:/c/Program Files/Eclipse Foundation/jdk-11.0.12.7-hotspot/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/Microsoft SQL Server/150/DTS/Binn:/c/Program Files/PuTTY:/c/Program Files/Calibre2:/c/Program Files/nodejs:/cmd:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/python38/Scripts:/c/python38:/c/Users/erjan/AppData/Local/Microsoft/WindowsApps:/c/Program Files/Azure Data Studio/bin:/c/Users/erjan/AppData/Local/Programs/Microsoft VS Code/bin:/c/Users/erjan/AppData/Roaming/npm:/c/Program Files/JetBrains/IntelliJ IDEA Community Edition 2020.3.2/bin:/c/kafka_2.12-2.8.0/bin/windows
由于您在 Windows 上并且没有 procfs,检索完整环境的最简单方法是使用 env
。
请注意,shell 是否会从环境中删除名称与合法 shell 变量名称不匹配的环境变量(因此也使它们对 env
等工具不可用)是实现定义的; POSIX sh 标准使得 shells 删除这些名称是合法的,但保留它们也是合法的,所以这是否有效可能因版本而异。
idea=$(env | sed -rne '/^IntelliJ IDEA Community Edition=/ { s/^[^=]+=//p }')
if [[ $idea ]]; then
echo "Found IntelliJ at: $idea"
else
echo "Could not find IntelliJ"
fi