# Strings comparing incorrectly

**URL:** <https://community.unix.com/t/strings-comparing-incorrectly/307967>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [April 7, 2012, 7:55pm UTC](https://community.unix.com/t/strings-comparing-incorrectly/307967 "2012-04-07T19:55:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SCB](https://community.unix.com/letter_avatar/scb/32/5_5575768a8748004e209b776fc1b2916d.png) [@SCB](https://community.unix.com/u/SCB)\
**Post date:** [April 7, 2012, 7:55pm UTC](https://community.unix.com/t/strings-comparing-incorrectly/307967/1 "2012-04-07T19:55:16Z")

</div>

Hello I'm very new to Linux and shell scripting so I only know basic stuff. I'm making a script with the purpose of finding the longest string or word in a file. Here's what I got so far:

```nohighlight
#!/bin/bash 
longest=""
for i in $(strings -n $1); do

if [[$longest < $i]]
then 
 longest=$i
fi
done
echo $longest

```

So basically the script will use a file given in the command line which contains words and then return the longest one.

This is the file I'm using to test it out it's called test:

```nohighlight
apples
are
the
best
in
the
world

```

now I run it with bash -x script2 test  
so I can see what's happening and this is what I get:

```nohighlight
+ longest=
++ strings -n 1 test
+ for i in '$(strings -n 1 $1)'
+ [['' < apples]]
+ longest=apples
+ for i in '$(strings -n 1 $1)'
+ [[apples < are]]
+ longest=are
+ for i in '$(strings -n 1 $1)'
+ [[are < the]]
+ longest=the
+ for i in '$(strings -n 1 $1)'
+ [[the < best]]
+ for i in '$(strings -n 1 $1)'
+ [[the < in]]
+ for i in '$(strings -n 1 $1)'
+ [[the < the]]
+ for i in '$(strings -n 1 $1)'
+ [[the < world]]
+ longest=world
+ echo world
world

```

So by the looks of it it's just replacing the value of the current word with the next one.  
So my question is how can I accurately compare the words so it returns the longest one?  
I'm guessing my mistake is in this line:

```nohighlight
if [[$longest < $i]]

```

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [April 7, 2012, 8:50pm UTC](https://community.unix.com/t/strings-comparing-incorrectly/307967/2 "2012-04-07T20:50:15Z")

</div>

```nohighlight
#! /bin/bash

longest=""
charCount=0

while read x
do
    if [$charCount -lt ${#x}]
    then
        charCount=${#x}
        longest=$x
    fi
done < $1

echo "Longest Word: $longest; Number of Chars: $charCount"

```

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [April 7, 2012, 11:04pm UTC](https://community.unix.com/t/strings-comparing-incorrectly/307967/3 "2012-04-07T23:04:21Z")

</div>

The more modern syntax is

```nohighlight
while read x
do
    if (( charCount < ${#x} ))
    then
        charCount=${#x}
        longest=$x
    fi
done < $1

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 8, 2012, 12:57am UTC](https://community.unix.com/t/strings-comparing-incorrectly/307967/4 "2012-04-08T00:57:17Z")

</div>

Minor nitpick:

```plaintext
done < "$1"

```

Otherwise it will break for filenames with spaces or strange characters..

> [@scb](#):
>
> [..]So by the looks of it it's just replacing the value of the current word with the next one.[..]

It is keeping the one that is lexically greater. If somehow the shorter string were padded with leading spaces to make them equal length, then your comparison would work, if you enclosed both variables in double quotes. But doing so would involve knowing the length of the larger of the two variables and that's what you were set to find out in the first place 🙂

```plaintext
$ [["bbb" < "aaaa"]] && echo hello
$ [[" bbb" < "aaaa"]] && echo hello
hello

```

---

<div class="post-metadata">

**Author:** ![SCB](https://community.unix.com/letter_avatar/scb/32/5_5575768a8748004e209b776fc1b2916d.png) [@SCB](https://community.unix.com/u/SCB)\
**Post date:** [April 8, 2012, 3:50am UTC](https://community.unix.com/t/strings-comparing-incorrectly/307967/5 "2012-04-08T03:50:09Z")

</div>

Works like a charm now. Thank you for the help :b:, didn't know it would be so much easier using a while loop.
