Ubuntu – How to execute commands in gnuplot using shell script

bashcommand linegnuplotscripts

What I want to do is write a script which first launches a program and then tells it to execute a bunch of commands and then quit. Lets go with an example.

I wrote this script myscript.sh and it doesn't work the way I want it to. What it does is just run gnuplot and wait for it to quit and then runs the other commands; which obviously produces errors.

#!/bin/bash
gnuplot
plot sin(x)
pause -1
quit

I guess it is clear what I'm trying to do; if not, then let me know in the comments.

Best Answer

From man gnuplot or its online manpage:

   -p,  --persist  lets  plot  windows  survive after main gnuplot program
   exits.

   -e "command list" executes the requested commands  before  loading  the
   next input file.

So what you probably want to run is the following command:

gnuplot -e "plot sin(x); pause -1"

Other variants I proposed but which are not that useful were:

gnuplot -p -e "plot sin(x); pause -1"
gnuplot -e "plot sin(x)"
gnuplot -p -e "plot sin(x)"