使用 /bin/bash
,我如何检测用户的 $PATH 变量中是否有特定目录?
例如
if [ -p "$HOME/bin" ]; then
echo "Your path is missing ~/bin, you might want to add it."
else
echo "Your path is correctly set"
fi
最佳答案
使用 grep
是矫枉过正,如果您正在搜索恰好包含 RE 元字符的任何内容,可能会造成麻烦。这个问题可以用 bash 的内置 [[
命令完美解决:
if [[ ":$PATH:" == *":$HOME/bin:"* ]]; then
echo "Your path is correctly set"
else
echo "Your path is missing ~/bin, you might want to add it."
fi
请注意,在 $PATH 的扩展和要搜索的路径之前添加冒号可以解决子字符串匹配问题;双引号路径避免了元字符的麻烦。
关于bash - 检测 PATH 中是否有特定的目录条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1396066/