0

I try to test if a string starts with a certain prefix. But my script seems not work (I would expect the "if" branch will not get run). Can some Bash expert help to take a look? thanks!

Here is my code and test result:

$ cat testb.bash

#!/bin/bash

my_var="abcdefg";
if [[ "${my_var:0:5}"=="order" ]]; then
    echo "value of my_var is ${my_var}.";
fi;

if [[ "${my_var:0:5}" -eq "order" ]]; then
    echo "value of my_var is ${my_var}.";
fi;

if [ "${my_var:0:5}"="order" ]; then
    echo "value of my_var is ${my_var}.";
fi;

$ bash -x testb.bash

+ my_var=abcdefg
+ [[ -n abcde==order ]]
+ echo 'value of my_var is abcdefg.'            
value of my_var is abcdefg.
+ [[ abcde -eq order ]]
+ echo 'value of my_var is abcdefg.'
value of my_var is abcdefg.
+ '[' abcde=order ']'
+ echo 'value of my_var is abcdefg.'
value of my_var is abcdefg.
$

4 Answers 4

2

Whitespace is significant in this case. As you can see in the -x output, it understands the first condition as

[[ -n "${my_var:0:5}==order" ]]

Moreover, to test for a prefix, you can use a pattern:

[[ $my_var == order* ]]
Sign up to request clarification or add additional context in comments.

Comments

2

To test the existence of substring, you can use either of these:

if [[ "$j" =~ string1 ]]; then
if [[ $j == *string1* ]]; then

In your particular case, you miss a space surounding ==, so instead of

if [[ "${my_var:0:5}"=="order" ]]; then

it should be

if [[ "${my_var:0:5}" == "order" ]]; then
                     ^  ^

Finally, note that your condition was evaluated as true because it was evaluating if [ "string" ], which is true if string is not empty:

$ [ "a" ] && echo "yes"
yes

Test

$ cat a
#!/bin/bash

my_var="abcdefg";
if [[ "${my_var:0:5}" == "order" ]]; then
    echo "value of my_var is ${my_var}."
elif [[ "${my_var:0:5}" == "abcde" ]]; then
    echo "yeahaa"
else
    echo "is not"
fi

$ ./a
yeahaa

3 Comments

Also you can remove useless semicolons
I guess just from my_var="abcdefg". In the if condition they are necessary before then.
You have an eagle-eye, @Aleks-DanielJakimenko :D
1

Ok, i tested your code, you shoud such as the following code:

prefix="pre_order";
pre="pre_"
len=${#pre}
echo $len
if [[ "${prefix:0:len}" == "blahvlah" ]] ;  then
 echo "dddd"
fi;

Notes:

  1. use == for string comparation
  2. for ${} you should initilize a string variable before ${}
  3. use len=${#pre} for lenght of string.

Comments

0

A POSIX-compliant way to test for a prefix is to attempt to remove the prefix, and compare the result to the original string. If the two are the same, the prefix is not present, the removal fails, and the expression expands to the original string.

prefix=foo
string=foobar

if [ "${string#$prefix}" = "$string" ]; then
    printf "$string does not start with $prefix\n"
else
    printf "$string starts with $prefix\n"
fi

Comments

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.