On page 329 (Hour 16) of your book Teach Yourself Unix in 24 Hours is the example:
test $value < $(echo $string | wc –c)
test `wc –l filename` -ge 10000
If I try this with this code:
value=3;string=”my horse Horace”
test $value < $(echo $string | wc -c)
test `wc -l ched4` -ge 10000
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:
would produce the actual date in the string. Make sense? That’s what should be happening above.
The notation then translates step by step:
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:
since the variable “value” has been set to 3, we’re really testing
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
and see if that works.
Let me know how it goes!