Capitalize first letter of string in Makefile using bash 4 syntax
Asked Answered
S

1

8

In bash 4 (and above), to capitalize the first letter of a string stored in variable L1, I can do the following:

L1=en
Ll1=${L1^}
echo $Ll1

This prints En.

I'm trying to do something similar within a Makefile, but I can't get the ${L1^} syntax to work.

SHELL := /bin/bash

L1 = en
Ll1 := $(shell echo ${L1^})

all:
    @echo $(Ll1)

Produces blank output.

Can I get this to work with this kind of bash syntax without resorting to tr/sed?

P.S. I do need to assign it to a variable and not echo it directly. I'm using bash 4.3.48 and GNU make 4.1.

Sloatman answered 1/5, 2018 at 9:40 Comment(1)
$(shell echo $${$(L1)^})Lorettalorette
M
9

You have two problems in your makefile, firstly the variable L1 is defined in make and isn't accessible in your call to your shell, use:

$(shell L1=$(L1); echo ...)

to define L1 in your shell.

The dollar sign needs to be escaped to not be interpreted by make:

$(shell L1=$(L1); echo $${L1^})
Malia answered 1/5, 2018 at 9:52 Comment(2)
This will give a bad substitution error due to this issue. To solve it you need to do bash -c 'echo $${L1^}' instead of just echo $${L1^}.Esoteric
@DonaldDuck It's a good point to be aware of what SHELL is defined to be, as the default is somthing like /bin/sh which might not be bash, but in this question OP already defined SHELL to be /bin/bashMalia

© 2022 - 2024 — McMap. All rights reserved.