如何在bash中测试字符串是否以另一个字符串开头?

Mas*_*low 9 bash git-bash

非常相似但不重复:https://stackoverflow.com/a/2172367/57883

我在Git Bash 3.1中(至少那是当我在git bash中键入bash时出现的提示.

$ test [["DEV-0" == D*]] || echo 'fail'打印失败.

if [['DEV-0-1' == DEV* ]]; then echo "yes";[[DEV-0-1: command not found 我试图测试是否git branch返回以DEV开头的东西.但我似乎无法应用答案.是因为我所有的尝试都在左边使用字符串文字而不是变量值?

我也在ideone http://ideone.com/3IyEND上尝试过它

没有运气 自从我提出linux提示以来已经有14年了.

我在字符串中缺少的是什么?

P.P*_*.P. 12

你错过了那里的空间:

 if [[ 'DEV-0-1' == DEV* ]]; then echo "yes"; fi
      ^^
Run Code Online (Sandbox Code Playgroud)


Ans*_*ers 6

我宁愿做这样的检查:

s1=DEV-0-1
s2=DEV

if [ "${s1:0:${#s2}}" == "$s2" ]; then
  echo "yes"
fi
Run Code Online (Sandbox Code Playgroud)