Answers:
尝试:
$ printf '%s\n' "${FOO//$WORDTOREMOVE/}"
CATS DOGS FISH
这也工作在ksh93
,mksh
,zsh
。
适当地:
FOO="CATS DOGS FISH MICE"
WORDTOREMOVE="MICE"
remove_word() (
set -f
IFS=' '
s=$1
w=$2
set -- $1
for arg do
shift
[ "$arg" = "$w" ] && continue
set -- "$@" "$arg"
done
printf '%s\n' "$*"
)
remove_word "$FOO" "$WORDTOREMOVE"
它假定您的单词是用空格分隔的,并且具有删除之前和之后的空格的副作用"$WORDTOREMOVE"
。