
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’? 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 Simpy.
Categorized:
Unix and Linux Help
(Article 7679)
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? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMI have a lot to say, but ...
I do have a comment, now that you mention it!
|
Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|