How do I test if a string starts with another in bash?

本小妞迷上赌 提交于 2019-12-10 03:10:49

问题


Very similar but not duplicate : https://stackoverflow.com/a/2172367/57883

I'm in Git Bash 3.1 (at least that's what comes up in the prompt when I type bash inside git bash.

and $ test [["DEV-0" == D*]] || echo 'fail' prints fail.

if [['DEV-0-1' == DEV* ]]; then echo "yes"; says [[DEV-0-1: command not found I'm trying to test if git branch returns something that starts with DEV. but I can't seem to apply the answer. is it because all my attempts are using a string literal on the left instead of a variable value?

I've also tried it on ideone http://ideone.com/3IyEND

and no luck. It's been ~14 years since I was good with a linux prompt.

What am I missing for a string starts with test in bash?


回答1:


You missed a space there:

 if [[ 'DEV-0-1' == DEV* ]]; then echo "yes"; fi
      ^^



回答2:


I'd probably rather do the check like this:

s1=DEV-0-1
s2=DEV

if [ "${s1:0:${#s2}}" == "$s2" ]; then
  echo "yes"
fi


来源:https://stackoverflow.com/questions/15418149/how-do-i-test-if-a-string-starts-with-another-in-bash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!