Bash To Test For Lenght Of A String


Recommended Posts

I am trying to use bash to test for the length of a string. Nothing seems to be making sence to me.

this code tests(and works) for a string larger then 7

#!/bin/bash
PASS=asdfghjkl
if  [[ `echo ${#PASS}` > 7 ]]
   then echo "works"
fi

but it fails with an error if I use '>=' like this. Why?

#!/bin/bash
PASS=asdfghjkl
if  [[ `echo ${#PASS}` >= 7 ]]
   then echo "works"
fi

I would think the following code would test for a string larger then 7 but smaller then 11, but this is not testing as true, it is testing as false

#!/bin/bash
PASS=asdfghjkl
if  [[ `echo ${#PASS}` > 7 ]] &&  [[ `echo ${#PASS}` < 11 ]]
   then echo "works"
fi

thanks for helping me sort this out

edit added later//

the problem seems to be specifically with this test. It is returning a 1, why is it not returning a 0 ?

#!/bin/bash
PASS=asdfghjkl
[[ `echo ${#PASS}` < 11 ]]
echo $?

Edited by shanenin
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...