【发布时间】:2019-10-22 14:22:04
【问题描述】:
如果我在变量中有一个 bash 字符串。 我如何提取/检索除最后一个字符之外的字符串,如果我想提取到最后两个字符会有多容易?
例子:
# Removing the last character
INPUT="This is my string."
# Expected output "This is my string"
# Removing the last two characters
INPUT="This is my stringoi"
# Expected output "This is my string"
【问题讨论】:
-
OUTPUT=${INPUT::-1}? -
在mac上我们不能使用负值
-
与 Mac 无关,是 Bash 版本。改用:
${OUTPUT::${#INPUT}-1}。 -
在这一点上,
/bin/bash已经过时了,应该被简单地视为 macOS 的 POSIX 兼容外壳。如果您想使用更新的bash功能,请安装最新版本或切换到zsh。