gpt4 book ai didi

bash - 提取点前的子字符串

转载 作者:行者123 更新时间:2023-11-29 09:17:29 26 4
gpt4 key购买 nike

我尝试在 bash 中减去 (.) 之前的第一个字符串。

例如:

1.2.3 -> 1
11.4.1 -> 11

我根据 docs 使用了以下命令:

s=4.5.0
echo "${s%.*}"

但它输出 4.5 而不是 4。我不明白。

这是为什么?

最佳答案

您需要使用 %% 从末尾删除最长 匹配项:

$ echo "${s%%.*}"
4

来自 the docs :

${parameter%%word}
Remove Largest Suffix Pattern. The word shall be expanded to produce a pattern. The parameter expansion shall then result in parameter, with the largest portion of the suffix matched by the pattern deleted.

关于bash - 提取点前的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40738346/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com