这是我在StackOverflow.com的原始问题
这是我写的脚本
#!/usr/bin/env bash GP=`/usr/bin/which git` PWD=`pwd` echo "PATH IS: ${GP}" echo "PWD IS: ${PWD}"
和输出是
PATH IS: PWD IS: /Users/user/tmp
所以问题是如何得到which git输出? 我在Mac OS X 10.6.2上运行它。
如果git不在$PATHvariables中列出的目录中,那么它将无法find它。
PATH受以下情况影响:
通常在/etc/profile指定。 也可能受到~/.bashrc对于非交互式shell和~/.bash_profile对loginshell的影响。
使用type (Bash内build)而不是which :
gp=$(type -P git)
如果您使用type -a ,则会显示别名,函数和多个可执行文件(如果存在替代项)。 如有必要,您可以parsing输出。
不要设置PWD – 它已经由Bash设置。 事实上,我build议不要使用全大写的variables名,以避免命名与Bashvariables的冲突。
# no need to set PWD, it's already set by Bash echo "PWD IS: ${PWD}"