Substitution and Command line macros

When a command line to gnuplot is first read, i.e. before it is interpreted or executed, two forms of lexical substitution are performed. These are triggered by the presence of text in backquotes (ascii character 96) or preceded by @ (ascii character 64).

Substitution of system commands in backquotes

Command-line substitution is specified by a system command enclosed in backquotes. This command is spawned and the output it produces replaces the backquoted text on the command line. Exit status of the system command is returned in variables GPVAL_SYSTEM_ERRNO and GPVAL_SYSTEM_ERRMSG.

Carriage-return (' \r') and newline (' \n') characters are stripped from the input stream during substitution. This is in distinction to the string returned by the system() function, which does not strip these characters. See system.

Command-line substitution can be used anywhere on the gnuplot command line, except inside strings delimited by single quotes.


This will run the program leastsq and replace leastsq (including backquotes) on the command line with its output:

f(x) = `leastsq`

or, in VMS

f(x) = `run leastsq`

These will generate labels with the current time and userid:

set label "generated on `date +%Y-%m-%d` by `whoami`" at 1,1
set timestamp "generated on %Y-%m-%d by `whoami`"

Substitution of string variables as macros

The character @ is used to trigger substitution of the current value of a string variable into the command line. The text in the string variable may contain any number of lexical elements. This allows string variables to be used as command line macros. Only string constants may be expanded using this mechanism, not string-valued expressions. For example:
style1 = "lines lt 4 lw 2"
style2 = "points lt 3 pt 5 ps 2"
range1 = "using 1:3"
range2 = "using 1:5"
plot "foo" @range1 with @style1, "bar" @range2 with @style2

The line containing @ symbols is expanded on input, so that by the time it is executed the effect is identical to having typed in full

plot "foo" using 1:3 with lines lt 4 lw 2, \
     "bar" using 1:5 with points lt 3 pt 5 ps 2

The function exists() may be useful in connection with macro evaluation. The following example checks that C can safely be expanded as the name of a user-defined variable:

C = "pi"
if (exists(C)) print C," = ", @C

Macro expansion does not occur inside either single or double quotes. However macro expansion does occur inside backquotes.

Macro expansion is handled as the very first thing the interpreter does when looking at a new line of commands and is only done once. Therefore, code like the following will execute correctly:

A = "c=1"

but this line will not, since the macro is defined on the same line and will not be expanded in time

A = "c=1"; @A   # will not expand to c=1

Macro expansion inside a bracketed iteration occurs before the loop is executed; i.e. @A will always act as the original value of A even if A itself is reassigned inside the loop.

For execution of complete commands the evaluate command may also be handy.

String variables, macros, and command line substitution

The interaction of string variables, backquotes and macro substitution is somewhat complicated. Backquotes do not block macro substitution, so
filename = "mydata.inp"
lines = ` wc --lines @filename | sed "s/ .*//" `

results in the number of lines in mydata.inp being stored in the integer variable lines. And double quotes do not block backquote substitution, so

mycomputer = "`uname -n`"

results in the string returned by the system command uname -n being stored in the string variable mycomputer.

However, macro substitution is not performed inside double quotes, so you cannot define a system command as a macro and then use both macro and backquote substitution at the same time.

machine_id = "uname -n"
mycomputer = "`@machine_id`"  # doesn't work!!

This fails because the double quotes prevent @machine_id from being interpreted as a macro. To store a system command as a macro and execute it later you must instead include the backquotes as part of the macro itself. This is accomplished by defining the macro as shown below. Notice that the sprintf format nests all three types of quotes.

machine_id = sprintf('"`uname -n`"')
mycomputer = @machine_id

Copyright 1986 - 1993, 1998, 2004 Thomas Williams, Colin Kelley
Distributed under the gnuplot license (rights to distribute modified versions are withheld).