Well, I have all the latest updates to BASH and all, and just upgraded my system a couple of months ago, so I know it can’t be a “backwards compatibility” problem. I tried this, and it returns:
bash: 11: command not found…
The “11″ IS the length of the string, and I tried it with different lengths which all are correct. I just don’t get where the alleged “error” is originating. Any ideas?
on 25 Jun 2011 at 11:37 am GMorris
Ahh, OK, I’ve figured this out. It works a lot better if you DO something with it, like “echo ${#str_var}”. I had used ‘expr length “$str_var”‘, so I was thinking along those lines as it returns the length without an ‘echo’. Duhh… I guess just because the ‘echo’ wasn’t in the post I assumed it would work the same way, but you know what they say about ‘ass u me’?
Now I’ve succeeded in becoming a true idiot, and I feel like it too!
Great! The web is full of crappy complicated methods for getting the string length. This is the most simple and elegant one.
I quote Alexander. Thank you!
Wow, this is awesome, thank you! Very elegant.
Thanks a lot..It helped
Well, I have all the latest updates to BASH and all, and just upgraded my system a couple of months ago, so I know it can’t be a “backwards compatibility” problem. I tried this, and it returns:
bash: 11: command not found…
The “11″ IS the length of the string, and I tried it with different lengths which all are correct. I just don’t get where the alleged “error” is originating. Any ideas?
Ahh, OK, I’ve figured this out. It works a lot better if you DO something with it, like “echo ${#str_var}”. I had used ‘expr length “$str_var”‘, so I was thinking along those lines as it returns the length without an ‘echo’. Duhh… I guess just because the ‘echo’ wasn’t in the post I assumed it would work the same way, but you know what they say about ‘ass u me’?
Now I’ve succeeded in becoming a true idiot, and I feel like it too!
thanks for this, simple and works.
I saw so many posts that were doing
echo $var | wc -c
but the method you posted is a lot more simpler