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.
Example:
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`"
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" @A
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.
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).