【发布时间】:2020-01-21 02:45:31
【问题描述】:
我是第一次尝试 zsh 的 bash 用户。在 bash 中,我具有操作路径的功能,例如仅在以下情况下将目录附加到路径中
- 该目录存在,并且
- 如果该目录当前不在路径中。
对于 bash,我有类似的东西:
# =============================================================================
# Returns true (0) if element is in the list, false (1) if not
# $1 = list, $2 = element
# =============================================================================
function lcontains() {
found=1 # 1=not found, 0=found
local IFS=:
for e in $1
do
if [[ $2 == $e ]]
then
found=0
break
fi
done
return $found
}
# =============================================================================
# Appends into a list an element
# $1 = list, $2 = element
# =============================================================================
function lappend() {
if [[ -d $2 ]] && ! lcontains "$1" "$2"
then
echo $1:$2
else
echo $1
fi
}
# Usage:
export PATH=$(lappend $PATH ~/bin)
# Add the same path again, and result in no duplication
export PATH=$(lappend $PATH ~/bin)
问题是,在 zsh 中,lcontains 函数不起作用,因为默认情况下 zsh 不分割空白。那么,有没有办法实现我的目标?
【问题讨论】:
标签: zsh