Free tech support / small logo


What does the $( ) notation mean in a Bash script?

On page 329 (Hour 16) of your book Teach Yourself Unix in 24 Hours is the example:

value=3 ; string="my horse Horace"
test $value < $(echo $string | wc –c)
test `wc –l filename` -ge 10000

If I try this with this code:
#!/bin/sh/
value=3;string="my horse Horace"
test $value < $(echo $string | wc -c)
test `wc -l ched4` -ge 10000

I get:-
syntax error at line 3: `(' unexpected

Are you able to quickly explain what the line test $value < $(echo $string | wc -c) is actually trying to do? I don’t quite understand the $(…) part … and is the < performing a ‘redirection for input’ or is it meant to be ‘less than’?


Dave's Answer:

The $( ) notation is a modern replacement for the ` ` (also called the "backtick") notation, both submit the contents to a subshell for execution and replace it with the output of that command. For example:

echo Today is $(date)

would produce the actual date in the string. Make sense? That’s what should be happening above.

The notation then translates step by step:

test $value < $(echo $string | wc -c)

then has the echo $string | wc -c replaced with the number of characters in the variable "string". That's going to be 15, so now we're testing:

test $value < 15

since the variable "value" has been set to 3, we're really testing

test 3 < 15

which should certainly be true!

I notice two additional things in your example, though. First, you have /bin/sh/ with the trailing slash, which might be a problem. But you also might not be finding that you have Bash as your default shell on your system too, so do you want to try

#!/bin/bash

and see if that works.

Let me know how it goes!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments

please let me know ,we type script in editor, i am using vi editor after that at command line we type :wq save and quit enter, we'll go to $ prompt then how to to see output for perticular script? is it shell dependent if yes let me know for borne,c-shell.please

Posted by: mini at December 18, 2007 11:40 PM

put a backslash before the < symbol in the test line, i.e.,
test $value \< $(echo $string | wc -c)


that way the shell will interpret it as a "less than" sign instead of an input redirection

Posted by: TonyC at June 14, 2008 12:55 PM

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!

Linux Journal: Free Issue!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.