2

How does -DQUOTE=yes work in the code below?

I expected the second line to be ifelse(yes,yes,Learn Linux today!)

The other parts make sense to me.

From Linux Pocket Guide - by Daniel J. Barrett

$ cat myfile 
My name is NAME and I am AGE years old.
ifelse(QUOTE,yes,Learn Linux today!)


$ m4 -DNAME=Sandy -DAGE=25 -DQUOTE=yes myfile
My name is Sandy and I am 25 years old.
Learn Linux today!
PurpleMongrel
  • 53
  • 1
  • 5

1 Answers1

4

The m4 processor recognizes the names of a number of built-in processing macros when they occur in the input, and ifelse is one of these.

It is common to use m4 with its -P option to avoid accidental processing of these instructions. Using -P requires you to prepend the string m4_ to all built-in instruction names that you want to actually process, e.g. m4_define, m4_ifelse, m4_dnl, etc.

This avoids most accidental executions of macro instructions in the input as no instruction that isn't prepended with m4_ would be treated as text.

$ cat file
My name is NAME and I am AGE years old.
ifelse(QUOTE,yes,Learn Linux today!)
$ m4 -P -DNAME=Sandy -DAGE=25 -DQUOTE=yes file
My name is Sandy and I am 25 years old.
ifelse(yes,yes,Learn Linux today!)

Without -P, ifelse is processed as the m4 multi-branch if-then-else instruction. This instruction is documented here. In short, if the first two arguments are equal, the whole instruction is replaced by the third argument, which is what you are seeing when you run your initial command: The 1st yes (expanded from the value of QUOTE) is equal to the 2nd yes, so Learn Linux today! is expanded. If you define QUOTE as another string, the ifelse instruction would be replaced by the 4th argument, but sine there isn't any 4th argument in your document, it would be replaced by nothing (i.e. removed).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936