11

I have a variable $projectName how can i check if it starts with this string 'testProject'

1
  • 1
    so glad nobody marked this question "repeated question" since only here could find the answer! Commented Mar 10, 2022 at 4:57

2 Answers 2

18

You can use this check in BASH:

[[ "$projectName" == "testProject"* ]]
Sign up to request clarification or add additional context in comments.

1 Comment

Finally something that worked!
11

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

2 Comments

The 2nd example $ var="ahello" $ [[ "$var" =~ ^he ]] && echo "yes" Should return 'no' because it's not starting with 'he'
@user3502786 well it wasn't returning anything because || was not defined. I just did for better understanding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.