-1

I have this test command:

test "ssh://hg@bitbucket.org/teros/moodwire_demo" == ssh*; echo $?

why does it exit with 1? I am trying to see if the first string starts with the second string.

3

The test command in bash can't do that. For strings, it only supports tests for equality, non-equality, and lexicographical ordering (see help test in bash).

You may however use

[[ "ssh://hg@bitbucket.org/teros/moodwire_demo" == ssh* ]]
echo "$?"

This form of string matching is only done within [[ ... ]] in bash.

Without using [[ ... ]]:

expr "ssh://hg@bitbucket.org/teros/moodwire_demo" : '^ssh' >/dev/null
echo "$?"

This uses the expr utility to match the (basic) regular expression ^ssh against the string. Since expr is not a built-in utility, this would be ever so slightly slower than using == within [[ ... ]] in bash.

  • ughh I dislike having to use double brackets :( I wonder if there is some way to get around it – Alexander Mills May 10 at 19:46
  • @Alexander Mills If you don't like double brackets, you can use grep for simple regexp tests like this: grep '^ssh' <<< 'ssh://hg@bitbucket.org/teros/moodwire_demo'; echo $? – Jim L. May 10 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.