Syntax error with use of pipe in bash

752 views Asked by At

I'm diving headfirst into bash with no prior experience and have hit a bit of a snag: I wrote a small bash script to determine the average of values (it's just a total right now) being returned by a c executable.

#Sample value of #s1total: 0+0.000117+0.000149+0.000106

printf "\n%s" $s1total

#The following line works
printf "\nTotal: %s\n" $(bc <<< $s1total)

#the following also works
echo
echo -n "Total: "
echo $s1total | bc

#The following line does not work
printf "\nTotal: %s\n" $($s1total|bc)

I did eventually find that the last line can be made to work by changing it to $(echo $s1total|bc) but i don't understand why that works ...

If I run as-is, I get an error:./sievetest.sh: line 25: 0+0.000117+0.000149+0.000106: command not found

Is the string being run before the pipe? Why? Why does adding "echo" fix it? How is the third method different than the first and second?

(as an aside, I thought the heredoc redirection operator was << , why the extra < ?)

1

There are 1 answers

2
Marc B On BEST ANSWER

You have this:

$($s1total|bc)

which is basically

$(0+0.000117+0.000149+0.000106 | bc)

e.g. run a command named 0+.... and pipe it to bc.

It should be

$(echo $s1total|bc)