A recent post here mentioned typecasting in the shell. I responded by advising the poster to use some other language for his scripting needs. My feeling is that while I do appreciate that Bash can be used for complicated tasks, I just don't see the point: you should use a language that matches your expectations for features. For me, that's Perl, but others might choose Python or Php or Java or who knows - but if you bring the right tool (or a tool you know well), jobs go much easier.

But back to bash and its typecasting. Bash does let you declare how you intend to use a variable. Let's do it "wrong" first:


#!/bin/bash
ralph="randols"
goo=$ralph
if [ $goo -lt 7 ]
then
echo $goo
fi

That script will fail with this message: "line 5: [: randols: integer expression expected". That's because line five ends up looking like this:

if [ "randols" -lt 7 ]

The "-lt" operator expects integer expressions on both sides, so it doesn't like its lunch and says so.

You can fix that:

#!/bin/bash
ralph="randols"
declare -i goo
goo=$ralph
if [ $goo -lt 7 ]
then
echo $goo
fi

At line two, we state our intentions for "goo" ("typeset -i goo" would do exactly the same thing). That works: $goo ends up with a value of 0, and is an integer, so the "-lt" is happy and the script runs. Everybody is happy, right? Naaw. Let's throw a monkey wrench in:

#!/bin/bash
ralph="10 randols"
declare -i goo
goo=$ralph
if [ $goo -lt 7 ]
then
echo $goo
fi

All we've done is change the initial value of "ralph", but our script has a fit:


./t: line 4: 10 randols: syntax error in expression (error token is "randols")
./t: line 5: [: -lt: unary operator expected

That's all it takes to break it. A similar Perl script doesn't even need all this nonsense (and doesn't confuse you by using text ("-lt") for arithmetic comparisons and arithmetic operators ("<") for string tests as Bash does):

#!/usr/bin/perl
$ralph="10 randols";

if ( $ralph < 7 ) {
print "$ralph\n";
}

There are, of course, ways around any problem. But why fight it? OK, if you just love Bash and are used to working around this kind of thing, fine: have at it. You can find problems with Perl, too, and anything else. My point is to use a language YOU are comfortable with, a language that meets your needs and expectations.

Don't struggle. Be happy. No silk purses from sows ears necessary.


Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Bash typecasting


3 comments



Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence







Tue Aug 1 12:58:10 2006: 2338   TonyLawrence

gravatar
Another minor point about the Perl script: the result of evaluating "10 randols" in an integer context is "10". That's often helpful in your scripts.. and yes, I know you can get the same effect in bash.. but that's not the point. The original poster didn't know much about Bash, so why get into it? If you have to learn scripting, learn with a "better" scripting language - better in the sense that it meets you expectations and needs of course.





Thu Feb 19 18:05:08 2009: 5469   JasonAntman

gravatar
What about the other way around? If I pass bash an IP address as an argument, it wants to evaluate it as an integer....





Thu Feb 19 18:07:03 2009: 5470   TonyLawrence

gravatar
Can you give a short example of what you are trying to do?

------------------------
Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

privacy policy