![为什么子字符串扩展 [ test="-efoo"; echo ${test:0:2} ] 失败?](https://linux22.com/image/129999/%E4%B8%BA%E4%BB%80%E4%B9%88%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%89%A9%E5%B1%95%20%5B%20test%3D%22-efoo%22%3B%20echo%20%24%7Btest%3A0%3A2%7D%20%5D%20%E5%A4%B1%E8%B4%A5%EF%BC%9F.png)
在 Linux Mint 上,使用 bash..
test="-ffoo"; echo ${test:0:2}
输出前两个字符的作品
但
test="-efoo"; echo ${test:0:2}
失败,输出显然为空。
我想这个的形式是
${parameter:offset:length}
我知道参数字符不能是*@#?-$!0_
但是$test
参数——它的内容肯定可以是任何东西吗?我猜-e
是触发了类似贝壳的东西,但为什么..
答案1
当你跑步时
test="-efoo"; echo ${test:0:2}
echo
与参数一起运行-e
,在某些echo
实现中,包括echo
大多数部署的内置命令bash
,它是一个有效的选项,因此被“吞没”。
test="-efoo"; printf %s\\n "${test:0:2}"