In GNU Make, how do I convert a variable to lowercase?

This is a dumb question, but .... with GNU Make:

VAR = MixedCaseText LOWER_VAR = $(VAR,lc) default: @echo $(VAR) @echo $(LOWER_VAR) 

In the above example, what is the correct syntax for converting the contents of a VAR to lowercase? The syntax shown (and everything else I came across) causes LOWER_VAR to be an empty string.

+41
makefile
Mar 20 '09 at 0:51
source share
4 answers

you can always print tr

 LOWER_VAR = `echo $(VAR) | tr AZ az` 

or

 LOWER_VAR = $(shell echo $(VAR) | tr AZ az) 

The 'lc' functions you are trying to call relate to the GNU Make Standard Library

Assuming it is installed, the correct syntax will be

 LOWER_VAR = $(call lc,$(VAR)) 
+38
Mar 20 '09 at 1:04
source share

You can do this directly in gmake without using the GNU Make Standard Library:

 lc = $(subst A,a,$(subst B,b,$(subst C,c,$(subst D,d,$(subst E,e,$(subst F,f,$(subst G,g,$(subst H,h,$(subst I,i,$(subst J,j,$(subst K,k,$(subst L,l,$(subst M,m,$(subst N,n,$(subst O,o,$(subst P,p,$(subst Q,q,$(subst R,r,$(subst S,s,$(subst T,t,$(subst U,u,$(subst V,v,$(subst W,w,$(subst X,x,$(subst Y,y,$(subst Z,z,$1)))))))))))))))))))))))))) VAR = MixedCaseText LOWER_VAR = $(call lc,$(VAR)) all: @echo $(VAR) @echo $(LOWER_VAR) 

He looks a little awkward, but he's doing his job.

If you come with a variety of $ (shell), use := instead of = , as in LOWER_VAR := $(shell echo $VAR | tr AZ az) . This way you only call the shell once a variable is declared, and not every time a variable is specified!

Hope this helps.

+40
Mar 20 '09 at 5:02
source share

To handle capital letters with accents:

 LOWER_VAR = $(shell echo $VAR | tr '[:upper:]' '[:lower:]') 

Results:

 $ VAR="Éclipse" $ echo $VAR | tr AZ az Éclipse $ echo $VAR | tr '[:upper:]' '[:lower:]' éclipse 
+14
Jun 09 '12 at 15:05
source share

I find this a little cleaner ...

 $(shell tr '[:upper:]' '[:lower:]' <<< $(VAR)) 
+3
Jul 25 '13 at 22:17
source share



All Articles