简体   繁体   中英

Bash - Check if the string starts with a predefined string (substring)

我有一个变量$projectName如何检查它是否以此字符串'testProject'开头

您可以在BASH中使用此检查:

[[ "$projectName" == "testProject"* ]]

You can for example use:

[[ "$projectName" =~ ^testProject ]] && echo "yes"
                     ^
                     beginning of line

Test

$ var="hello"
$ [[ "$var" =~ ^he ]] && echo "yes" || echo "no"
yes
$ var="ahello"
$ [[ "$var" =~ ^he ]] && echo "yes" || echo "no"
no

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM