I want to use gnuplot to draw figure from data file, say foo.data. Currently, I hardcoded the data file name in the command file, say foo.plt, and run command gnuplot foo.plg
to plot data. However, I want to pass the data file name as a command argument, e.g. running command gnuplot foo.plg foo.data
. How to parse the command line arguments in gnuplot script file? Thanks.

- 4,256
- 7
- 27
- 36
10 Answers
You can input variables via switch -e
$ gnuplot -e "filename='foo.data'" foo.plg
In foo.plg you can then use that variable
$ cat foo.plg
plot filename
pause -1
To make "foo.plg" a bit more generic, use a conditional:
if (!exists("filename")) filename='default.dat'
plot filename
pause -1
Note that -e
has to precede the filename otherwise the file runs before the -e
statements. In particular, running a shebang gnuplot #!/usr/bin/env gnuplot
with ./foo.plg -e ...
CLI arguments will ignore use the arguments provided.

- 347,512
- 102
- 1,199
- 985

- 2,617
- 1
- 15
- 8
-
20This also can be used with `if` to supply defaults. `if ! exists("filename") filename='default.data'` – mgilson Sep 09 '12 at 17:11
-
10For people stumbling across this now, mgilson's comment and Gnuplot 4.6 patchlevel 3 require `if (!exists("filename")` instead of `if ! exists("filename")`. – Sam Mussmann Aug 25 '13 at 19:33
-
How to do the same thing in windows? – padawan Jun 20 '14 at 17:41
-
I've written a short example of the environment variable passing method here: http://41j.com/blog/2011/10/grabbing-environment-variables-in-gnuplot/ – new299 Mar 04 '15 at 15:30
-
What if there are multiple arguments needed to be passed in? – Chong Dec 14 '15 at 06:19
-
6@Chong: Either string them together or give each one in an `-e` argument, e.g. `-e "filename='default.data'; foo='bar'"` or `-e "filename='default.data'" -e "foo='bar"'`. – Thor Feb 02 '16 at 16:37
-
1__Note:__ The `-e ...` option has to precede the filename. Indeed from `man gnuplot` we can read: _`-e` "command list" executes the requested commands __before loading the next input file__._. – Hastur May 03 '17 at 16:18
-
The single quotes inside the double quotes are important - otherwise it won't be parsed as a string literal. – z0r Feb 07 '19 at 09:38
You can pass arguments to a gnuplot script since version 5.0, with the flag -c
. These arguments are accessed through the variables ARG0
to ARG9
, ARG0
being the script, and ARG1
to ARG9
string variables. The number of arguments is given by ARGC
.
For example, the following script ("script.gp")
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
can be called as:
$ gnuplot -c script.gp one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
or within gnuplot as
gnuplot> call 'script.gp' one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
In gnuplot 4.6.6 and earlier, there exists a call
mechanism with a different (now deprecated) syntax. The arguments are accessed through $#
, $0
,...,$9
. For example, the same script above looks like:
#!/usr/bin/gnuplot --persist
THIRD="$2"
print "first argument : ", "$0"
print "second argument : ", "$1"
print "third argument : ", THIRD
print "number of arguments: ", "$#"
and it is called within gnuplot as (remember, version <4.6.6)
gnuplot> call 'script4.gp' one two three four five
first argument : one
second argument : two
third argument : three
number of arguments: 5
Notice there is no variable for the script name, so $0
is the first argument, and the variables are called within quotes. There is no way to use this directly from the command line, only through tricks as the one suggested by @con-fu-se.

- 2,372
- 20
- 30
-
I tried to use the `-c` switch with a script that contains lines such as `plot 'data' using 0:($1/100)`. `gnuplot` dies with the error _invalid expression_ because `$1` is disappeared. Where am I wrong? Note that without `-c`, the script runs succefully. – lorcap Sep 30 '15 at 15:43
-
I tested it with `gnuplot 5.0` by adding to these examples a line like `plot 'data' using 0:($1/100)`, and didn't get what you say. It would be rare, since this version defines the variables `ARG0`--`ARG9`, and not `$1`--`$9`. I assume you also have version 5.0 (do you?), since the `-c` flag is not supported by earlier versions. I would need to see a minimal script to see where is the real problem :/ – vagoberto Sep 30 '15 at 16:19
-
I'm running `gnuplot 5.0 patchlevel 0` under [cygwin](http://cygwin.com/). The one-line script `print ARG1; plot 'data' using 0:($1/100)` correctly print the value passed as `ARG1` and then exits with the error _invalid expression_ pointing at the slash of `print ARG1; plot 'data' using 0:(/100)`. – lorcap Oct 01 '15 at 11:12
You can also pass information in through the environment as is suggested here. The example by Ismail Amin is repeated here:
In the shell:
export name=plot_data_file
In a Gnuplot script:
#! /usr/bin/gnuplot
name=system("echo $name")
set title name
plot name using ($16 * 8):20 with linespoints notitle
pause -1

- 45,082
- 11
- 119
- 130
-
6Instead of export, you can type `name=plot_data_file ./the_gnuplot_script`. – Edgar Bonet Oct 27 '15 at 14:41
-
@Edgar Bonet: Which gnuplot version is required for this? It does not seem to work out on y machine which uses 4.2... – Bjoern Feb 02 '16 at 13:30
-
Hi @Bjoern! I am using Gnuplot 4.6, but this is not a Gnuplot feature, it a feature of the shell: `name=value command` tells the shell to run the command with that particular variable in its environment. I am using bash 4.3.11, but I believe it's a very common shell feature. – Edgar Bonet Feb 03 '16 at 21:03
The answer of Jari Laamanen is the best solution. I want just explain how to use more than 1 input parameter with shell variables:
output=test1.png
data=foo.data
gnuplot -e "datafile='${data}'; outputname='${output}'" foo.plg
and foo.plg:
set terminal png
set outputname
f(x) = sin(x)
plot datafile
As you can see,more parameters are passed with semi colons (like in bash scripts), but string variables NEED to be encapsuled with ' ' (gnuplot syntax, NOT Bash syntax)

- 642
- 1
- 8
- 18
-
1For gnuplot itself it doesn't matter if you use `'` or `"` for strings. The outer quotes around the `-e` argument must be `"` to have the bash variables substituted. The following also works fine: `OUTPUT=foobar; gnuplot -e "output=\"$OUTPUT\"; print output"` – Christoph Jun 18 '14 at 05:52
-
Thanks for confirming that I can use semi colons to split commands. None of the other answers show this. – tommy.carstensen Feb 20 '15 at 09:54
-
Just a minor mistake in your code: it should be `set output outputname` instead of just `set outputname`. – pulsar_hh Apr 12 '22 at 10:15
You may use trick in unix/linux environment:
in gnuplot program: plot "/dev/stdin" ...
In command line: gnuplot program.plot < data.dat

- 139
- 1
- 2
This question is well answered but I think I can find a niche to fill here regardless, if only to reduce the workload on somebody googling this like I did. The answer from vagoberto gave me what I needed to solve my version of this problem and so I'll share my solution here.
I developed a plot script in an up-to-date environment which allowed me to do:
#!/usr/bin/gnuplot -c
set terminal png truecolor transparent crop
set output ARG1
set size 1, 0.2
rrLower = ARG2
rrUpper = ARG3
rrSD = ARG4
resultx = ARG5+0 # Type coercion required for data series
resulty = 0.02 # fixed
# etc.
This executes perfectly well from command-line in an environment with a recent gnuplot (5.0.3 in my case).
$ ./plotStuff.gp 'output.png' 2.3 6.7 4.3 7
When uploaded to my server and executed, it failed because the server version was 4.6.4 (current on Ubuntu 14.04 LTS). The below shim solved this problem without requiring any change to the original script.
#!/bin/bash
# GPlot v<4.6.6 doesn't support direct command line arguments.
#This script backfills the functionality transparently.
SCRIPT="plotStuff.gp"
ARG1=$1
ARG2=$2
ARG3=$3
ARG4=$4
ARG5=$5
ARG6=$6
gnuplot -e "ARG1='${ARG1}'; ARG2='${ARG2}'; ARG3='${ARG3}'; ARG4='${ARG4}'; ARG5='${ARG5}'; ARG6='${ARG6}'" $SCRIPT
The combination of these two scripts allows parameters to be passed from bash to gnuplot scripts without regard to the gnuplot version and in basically any *nix.

- 240
- 2
- 10
@vagoberto's answer seems the best IMHO if you need positional arguments, and I have a small improvement to add.
vagoberto's suggestion:
#!/usr/local/bin/gnuplot --persist
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
which gets called by:
$ gnuplot -c script.gp one two three four five
script name : script.gp
first argument : one
third argument : three
number of arguments: 5
for those lazy typers like myself, one could make the script executable (chmod 755 script.gp
)
then use the following:
#!/usr/bin/env gnuplot -c
THIRD=ARG3
print "script name : ", ARG0
print "first argument : ", ARG1
print "third argument : ", THIRD
print "number of arguments: ", ARGC
and execute it as:
$ ./imb.plot a b c d
script name : ./imb.plot
first argument : a
third argument : c
number of arguments: 4

- 743
- 6
- 18
You could even do some shell magic, e.g. like this:
#!/bin/bash
inputfile="${1}" #you could even do some getopt magic here...
################################################################################
## generate a gnuplotscript, strip off bash header
gnuplotscript=$(mktemp /tmp/gnuplot_cmd_$(basename "${0}").XXXXXX.gnuplot)
firstline=$(grep -m 1 -n "^#!/usr/bin/gnuplot" "${0}")
firstline=${firstline%%:*} #remove everything after the colon
sed -e "1,${firstline}d" < "${0}" > "${gnuplotscript}"
################################################################################
## run gnuplot
/usr/bin/gnuplot -e "inputfile=\"${inputfile}\"" "${gnuplotscript}"
status=$?
if [[ ${status} -ne 0 ]] ; then
echo "ERROR: gnuplot returned with exit status $?"
fi
################################################################################
## cleanup and exit
rm -f "${gnuplotscript}"
exit ${status}
#!/usr/bin/gnuplot
plot inputfile using 1:4 with linespoints
#... or whatever you want
My implementation is a bit more complex (e.g. replacing some magic tokens in the sed call, while I am already at it...), but I simplified this example for better understanding. You could also make it even simpler.... YMMV.

- 51
- 1
- 1
In the shell write
gnuplot -persist -e "plot filename1.dat,filename2.dat"
and consecutively the files you want. -persist is used to make the gnuplot screen stay as long as the user doesn't exit it manually.

- 399
- 6
- 19
-
In Win7 console, the option -persist seems not work? For example, `gnuplot -persist -c "myscript.plt" "mydata.csv" "myoutput.png"`, this command works all right and I get the file "myoutput.png" as I expect, but the gnuplot screen does not appear(NO `exit` command in myscript.plt). Why? And how to make the gnuplot screen showes in my example? – lyl Jul 09 '19 at 07:43
Yet another way is this:
You have a gnuplot script named scriptname.gp
:
#!/usr/bin/gnuplot -p
# This code is in the file 'scriptname.gp'
EPATH = $0
FILENAME = $1
plot FILENAME
Now you can call the gnuplot script scriptname.gp
by this convoluted peace of syntax:
echo "call \"scriptname.gp\" \"'.'\" \"'data.dat'\"" | gnuplot

- 3,772
- 5
- 39
- 60