shell字符串截取报错:Bad substitution的解决

2022-06-19 17:33:42

这篇文章主要介绍了shell字符串截取报错:Bad substitution的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

问题重现:

当前系统Ubuntu16

shell进行字符串截取

运行 ${GIT_COMMIT:0:6}报错:

/tmp/jenkins7505855461406722507.sh: 4: /tmp/jenkins7505855461406722507.sh: Bad substitution

原因:两种shell语言解释器 bash和dash

查看当前/bin/sh 指向哪种解释器:

ls /bin/sh -al

lrwxrwxrwx http://www.cppcns.com1 root root 4 Mar 26 2018 /bin/sh -> dash

修改bin/sh指向为bash:

sudo ln -fs /bin/bash /bin/sh

或者运行 sudo dpkg-reconfigure dash 后选择NO

或者修改表达式 ${GIT_COMMIT:0:6}为:

$(echo $GIT_COMMIT | awk '{ string=substr($0,0,6); print string; }')

到此这篇关于shell字符串截取报错:Bad substitution的解决的文章就介绍到这了,更多相关shell字符串截取报错内容请搜索www.cppcns.com我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!