2

我的代码

FOO="dsad; dsadasddas; dadsad; dsdsad asdsdd"
echo "${FOO%%;*}"   
result: dsad

FOO="dsad; dsadasddas; dadsad; dsdsad asdsdd"
echo "${FOO##*;}"
result: dadsad; dsdsad asdsdd

如何打印第一个分号的所有字符?

FOO="dsad; dsadasddas; dadsad; dsdsad asdsdd"
echo "${FOO???}"
result: dsadasddas; dadsad; dsdsad asdsdd

如何打印最后一个分号之前的所有字符?

FOO="dsad; dsadasddas; dadsad; dsdsad asdsdd"
echo "${FOO???}"
result: dsadasddas; dadsad; dsdsad asdsdd

帮助任何人?谢谢 ;)

4

1 回答 1

2

使用#and%代替##and %%

foo='one; two; three; four'

# All characters after first semicolon
echo "${foo#*;}"  # two; three; four

# All characters before last semicolon
echo "${foo%;*}"  # one; two; three

${var##glob}并且${var%%glob}会尽可能的带走(贪心匹配)。${var#glob}并且${var%glob}会尽可能少地带走(不情愿的匹配)。

于 2013-03-07T02:29:19.020 回答