Industry guru Dave Taylor offers tech support on technical and business topics, including iPhone, iPod, Microsoft Windows, Sony PSP, cellphones, online advertising, CSS, Web design, business, Unix, Linux, SEO, Mac OS X, and shell script programming.     


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!


More Useful Unix and Linux Help Articles:
✔   Copy and Paste from the Mac OS X Command Line?
I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔   Shell script to convert lowercase to title case?
As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
✔   Can I script renaming files based on an XML data map?
I have a folder full of files which are named with four digits and a file extension e.g. 0312.file and an XML-file describing...
✔   Test for valid numbers in a Bash shell script?
In a different discussion on this site [see Redirecting input in a shell script] a visitor commented that "I was too busy trying...
✔   Review: iSSH for the iPad/iPhone
If you're running an online business like I am, there are times when you need to connect and log in to the server...

Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
    Enter your name: and your email addr:  





Categorized: Unix and Linux Help   (Article 7679, Written by )
Tagged: linux, shell script programming, unix
Previous: Why does Internet Explorer block all my pop-up windows?
Next: What cool things can I do with Twitter?




Reader Comments To Date: 2

mini said, on December 18, 2007 11:40 PM:

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

TonyC said, on June 14, 2008 12:55 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

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, Dave, for all your helpful information by buying you a cup of coffee!

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











I will never send you any unsolicited email. Ever.






Check This Out Too...

 
Look for Answers
Need Help? Ask Dave Taylor!
Powered By
Linux Journal: Free Issue!


Follow Me on Pinterest

Find Me on Google+
ADT on G+
© 2002 - 2013 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. Further, 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. My lawyer says "Thanks".
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.