Start an interactive session in gnuplot and execute some commands when it opens

by Gilles Castel   Last Updated May 15, 2018 20:01 PM

When I execute my script, it should

  1. Open a terminal with gnuplot (interactive session)
  2. Execute some gnuplot commands.
  3. The interactive session stays open. (user can execute commands)

I've tried the -e flag which executes code, but the terminal closes and is not interactive.

The -p flag lets the plot window survive, which is less than I need.

I've also tried loading a gnuplot script in the -e-flag, hoping that that would start an interactive session, but no avail.

My current workaround is sending keystrokes using xte, which works, but is a bit hacky. Are there better solutions?

Answers 3

The following script will do what you are asking as I understand it. It will run a sequence of gnuplot commands non-interactively and then continue the same gnuplot session from the command line. The comments in the script indicate where you should put the non-interactive commands.

#!/bin/bash - 

# Cheap 'trick' to show gnuplot prompt
# There are better ways
prmpt () { (echo -n "gnuplot> " >&2) }

# Function that serves as a pipe to gnuplot; 
# non-interactively then interactively
gnuplotInPipe () {
  # simple example from
  # All non interactive stuff goes here:
  echo "set title \"Simple Plots\" font \",20\""
  echo "set key left box"
  echo "set samples 50"
  echo "set style data points"
  echo "plot [-10:10] sin(x),atan(x),cos(atan(x))"
  # end of non-interactive gnuplot commands
  (echo "Type 'quit' to exit" >&2)
  # Here we read in lines (terminated with newline)
  # and pipe them directly to the same gnuplot
  # session
  while true; do
    read -er cmd
    if [ "$cmd" == 'quit' ]; then
    echo "$cmd"

# Call gnuplotInPipe and pipe it's output
# to gnuplot
gnuplotInPipe | gnuplot 


  1. The method I use to emulate the gnuplot prompt is a quick hack which can undoubtedly be improved and is actually completely unnecessary.

  2. If this isn't what you are trying to do, please provide the commands you are running (have tried) from the terminal as well as a sufficient excerpt of the gnuplot script you are calling.

  3. To improve on this, take a look at named pipes. If you want to maintain a script like this for long term use, I would go that route for maintainability reasons. This script is getting a very similar result with more 'brute' force methods.

    For example, I'm using stderr for things not going to gnuplot as a convenience - the prompt, the quit command info. Those would be better handled with a named pipe.

July 05, 2016 05:51 AM

If you're on a *nix system (I'm using Linux), then everything looks like a file, which means we can read our current terminal session just like we can a normal file.

I placed the following at the end of my gnuplot script to get the name of my terminal, and 'load' it just as you would a normal script:

set title 'Interactive session'
plot 'mydata.dat'
tty=system("tty")   # execute the command "tty" in OS and store output
load tty            # load the tty like a file

You don't get any prompt this way, but it is working well for my purposes. You can continue typing in the terminal; hitting Enter (or Return, or whatever your end-of-line key is called) will send the line to the gnuplot session.  Ctrl+D will exit the session as normal.

March 23, 2018 19:32 PM

gnuplot -e "print 'here I am'" -

should work

May 15, 2018 19:25 PM

Related Questions

Loop Files, Get Next Number

Updated February 17, 2017 22:01 PM

gnome-terminal - color palette

Updated April 26, 2015 01:00 AM

Bash prompt "variables" not working?

Updated June 17, 2017 03:01 AM

How do I detect when a terminal gets focus in Linux?

Updated August 01, 2017 19:01 PM