TCL
有个特殊变量叫做env
,用来保存环境变量的数据。
网上有提供输出此环境变量的方法,但是存在一定的局限性,下面提供了一种方法,输出env
的键值对
#!/usr/bin/tclsh
foreach key [array names env] {
puts "key: $key, value: $env($key)"
}
puts "env size: [array size env]"
puts "LANG: $env(LANG)"
打印输出为:
fh@Feihu-3 learn % ./array.tcl
key: VSCODE_GIT_ASKPASS_MAIN, value: /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js
key: VSCODE_GIT_ASKPASS_NODE, value: /Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer)
key: HOMEBREW_BOTTLE_DOMAIN, value: https://mirrors.ustc.edu.cn/homebrew-bottles
key: HOME, value: /Users/fh
key: LANG, value: zh_CN.UTF-8
key: PWD, value: /Users/fh/fh_data/workspace/tcl/learn
key: COMMAND_MODE, value: unix2003
key: LOGNAME, value: fh
key: XPC_SERVICE_NAME, value: 0
key: SSH_AUTH_SOCK, value: /private/tmp/com.apple.launchd.jUsrReVWLO/Listeners
key: COLORTERM, value: truecolor
key: TERM_PROGRAM, value: vscode
key: __CF_USER_TEXT_ENCODING, value: 0x1F5:0x19:0x34
key: XPC_FLAGS, value: 0x0
key: TERM, value: xterm-256color
key: SHELL, value: /bin/zsh
key: TMPDIR, value: /var/folders/8m/dwtpjrx55ks5tbh3x08ln6wh0000gn/T/
key: GIT_ASKPASS, value: /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh
key: SHLVL, value: 1
key: OLDPWD, value: /Users/fh
key: ORIGINAL_XDG_CURRENT_DESKTOP, value: undefined
key: TERM_PROGRAM_VERSION, value: 1.61.1
key: __CFBundleIdentifier, value: com.microsoft.VSCode
key: VSCODE_GIT_IPC_HANDLE, value: /var/folders/8m/dwtpjrx55ks5tbh3x08ln6wh0000gn/T/vscode-git-0c84f9610c.sock
key: PATH, value: /Users/fh/Library/Python/2.7/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin:/Applications/Wireshark.app/Contents/MacOS:/Users/fh/Library/Python/2.7/bin:/usr/local/sbin
key: _, value: /Users/fh/fh_data/workspace/tcl/learn/./array.tcl
key: USER, value: fh
env size: 27
LANG: zh_CN.UTF-8
fh@Feihu-3 learn %
最后参考,网上打印env
的方法:
#!/usr/bin/tclsh
puts "-----------------------------------------------"
puts [array get env]
puts "-----------------------------------------------"
parray env
fh@Feihu-3 learn % ./array.tcl
-----------------------------------------------
VSCODE_GIT_ASKPASS_MAIN {/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js} VSCODE_GIT_ASKPASS_NODE {/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer)} HOMEBREW_BOTTLE_DOMAIN https://mirrors.ustc.edu.cn/homebrew-bottles HOME /Users/fh LANG zh_CN.UTF-8 PWD /Users/fh/fh_data/workspace/tcl/learn COMMAND_MODE unix2003 LOGNAME fh XPC_SERVICE_NAME 0 SSH_AUTH_SOCK /private/tmp/com.apple.launchd.jUsrReVWLO/Listeners COLORTERM truecolor TERM_PROGRAM vscode __CF_USER_TEXT_ENCODING 0x1F5:0x19:0x34 XPC_FLAGS 0x0 TERM xterm-256color SHELL /bin/zsh TMPDIR /var/folders/8m/dwtpjrx55ks5tbh3x08ln6wh0000gn/T/ GIT_ASKPASS {/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh} SHLVL 1 OLDPWD /Users/fh ORIGINAL_XDG_CURRENT_DESKTOP undefined TERM_PROGRAM_VERSION 1.61.1 __CFBundleIdentifier com.microsoft.VSCode VSCODE_GIT_IPC_HANDLE /var/folders/8m/dwtpjrx55ks5tbh3x08ln6wh0000gn/T/vscode-git-0c84f9610c.sock PATH /Users/fh/Library/Python/2.7/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin:/Applications/Wireshark.app/Contents/MacOS:/Users/fh/Library/Python/2.7/bin:/usr/local/sbin _ /Users/fh/fh_data/workspace/tcl/learn/./array.tcl USER fh
-----------------------------------------------
env(COLORTERM) = truecolor
env(COMMAND_MODE) = unix2003
env(GIT_ASKPASS) = /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh
env(HOME) = /Users/fh
env(HOMEBREW_BOTTLE_DOMAIN) = https://mirrors.ustc.edu.cn/homebrew-bottles
env(LANG) = zh_CN.UTF-8
env(LOGNAME) = fh
env(OLDPWD) = /Users/fh
env(ORIGINAL_XDG_CURRENT_DESKTOP) = undefined
env(PATH) = /Users/fh/Library/Python/2.7/bin:/usr/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Library/Apple/usr/bin:/Applications/Wireshark.app/Contents/MacOS:/Users/fh/Library/Python/2.7/bin:/usr/local/sbin
env(PWD) = /Users/fh/fh_data/workspace/tcl/learn
env(SHELL) = /bin/zsh
env(SHLVL) = 1
env(SSH_AUTH_SOCK) = /private/tmp/com.apple.launchd.jUsrReVWLO/Listeners
env(TERM) = xterm-256color
env(TERM_PROGRAM) = vscode
env(TERM_PROGRAM_VERSION) = 1.61.1
env(TMPDIR) = /var/folders/8m/dwtpjrx55ks5tbh3x08ln6wh0000gn/T/
env(USER) = fh
env(VSCODE_GIT_ASKPASS_MAIN) = /Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js
env(VSCODE_GIT_ASKPASS_NODE) = /Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Renderer).app/Contents/MacOS/Code Helper (Renderer)
env(VSCODE_GIT_IPC_HANDLE) = /var/folders/8m/dwtpjrx55ks5tbh3x08ln6wh0000gn/T/vscode-git-0c84f9610c.sock
env(XPC_FLAGS) = 0x0
env(XPC_SERVICE_NAME) = 0
env(_) = /Users/fh/fh_data/workspace/tcl/learn/./array.tcl
env(__CFBundleIdentifier) = com.microsoft.VSCode
env(__CF_USER_TEXT_ENCODING) = 0x1F5:0x19:0x34
fh@Feihu-3 learn %
从上可知,使用parray
和[array get env]
方法适合调试,但是在使用上存在一定局限性。