bash - Shell scripting - which word is first alphabetically? -



bash - Shell scripting - which word is first alphabetically? -

how can check words first alphabetically between 2 words? illustration in code

#!/bin/bash var1="apple" var2="bye" if [ $var1 \> $var2 ] echo $var1 else echo $var2 fi

i want print apple, since apple comes before bye alphabetically, isnt working intended. doing wrong?

the bash builtin [ allow string comparisons (it's 1 of primaries listed under conditional expressions in bash manpage) have escape them they're not treated output redirection commands.

this leads rather ugly looking code. lines you'd expect work:

var1=apple var2=bye if [ $var1 > $var2 ]

would evaluate to:

if [ 'apple' ]

(which true) while creating file called bye in current directory. in fact, since you'd have reverse sense right, you're more see message:

./pax.sh: line 7: bye: no such file or directory

simply reversing sense of statement working correctly, since want output apple if it's less bye:

if [ $var1 \< $var2 ]

alternatively, can utilize [[ variant doesn't require escaping:

if [[ $var1 < $var2 ]]

i prefer latter because:

it looks nicer; and the [[ variant much more expressive , powerful.

bash shell alphabetical

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -