Issue
The following command is working fine when I type it in Bash (it generates histogram):
$ cat input.dat | gnuplot -p -e 'set style data histograms; set style histogram cluster gap 1; set style fill solid border -1; set boxwidth 0.05; binwidth=0.1 ; bin(x,width)=width*floor(x/width) + binwidth/2.0; plot "-" using (bin($1, binwidth)):(1) smooth freq with boxes'
I would like to put this command in a file and on several lines. I tried for this:
#!/bin/bash
cat input.dat | gnuplot -p -e 'set style data histograms; set style histogram cluster gap 1; \
bin(x,width)=width*floor(x/width) + binwidth/2.0; \
plot "-" using (bin($1, binwidth)):(1) smooth freq with boxes'
but when I execute this shell script, I get the following error:
set style data histograms; set style histogram cluster gap 1; \
^
line -3: invalid character \
The issue is that I divide gnuplot -p -e
command on multiple lines but between simple quotes '... set style histogram cluster gap 1; \ ... bin(x,width)=width*floor(x/width) + binwidth/2.0; \ ... smooth freq with boxes'
.
This is not the same situation as when we divide linux command with backslash: in my case, I divide inside block of 2 simple quotes.
Is there a way to circumvent this problem and split the first command above?
Solution
If you switch the single quotes surrounding the expression with double quotes, and the double quotes surrounding the - with single quotes, you can escape the $1 and the script should work:
cat input.dat | gnuplot -p -e "set style data histograms; set style histogram cluster gap 1; \
set style fill solid border -1; set boxwidth 0.05; binwidth=0.1 ; \
bin(x,width)=width*floor(x/width) + binwidth/2.0; \
plot '-' using (bin(\$1, binwidth)):(1) smooth freq with boxes"
Answered By - Edilaic