Previous: Substitution_of_string_variables_as_macros, Up: Substitution_and_Command_line_macros


1.21.3 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