Industry guru Dave Taylor offers free tech support on a wide variety of technical and business topics, including HTML, Apple iPhone, online advertising, Cascading Style Sheets, Web design, management, Unix, Linux, search engine optimization, online dating, Mac OS X, shell script programming and Microsoft Windows.

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!



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Stumble Upon    

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

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 a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

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











Remember personal info?


Please note that I will never send you any unsolicited commercial 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.









Uniblue: Free Virus Scan

Follow me on Twitter @DaveTaylor

Search
Find just the answers you seek from among our 2300+ free tech support articles by using our Lijit search engine.


Linux Journal: Free Issue!

Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Book Links
© 2002 - 2009 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.