Get the first letter of the make variable

Is there a better way to get the first character of a GNU make variable than

 FIRST=$(shell echo $(VARIABLE) | head -c 1) 

(which is not only bulky, but also causes an outer shell)?

+6
source share
3 answers

This is pretty terrible, but at least it doesn't call the shell :

 $(eval REMAINDER := $$$(VAR)) # variable minus the first char FIRST := $(subst $(REMAINDER),,$(VAR)) # variable minus that 
+7
source

GNU Make Standard Library provides substr function

zbz

 Arguments: 1: A string 2: Start offset (first character is 1) 3: Ending offset (inclusive) Returns: Returns a substring 

I have not tested it, but $(call substr,$(VARIABLE),1,1) should work

+2
source

Since I stumbled upon this in my own search and did not find what I was looking for, here is what I ended up using to parse a hexadecimal number that can be applied to any known character set

 letters := 0 1 2 3 4 5 6 7 8 9 abcdef nextletter = $(strip $(foreach v,$(letters),$(word 2,$(filter $(1)$(v)%,$(2)) $v))) 

then

 INPUT := 40b3 firstletter := $(call nextletter,,$(INPUT)) secondletter := $(call nextletter,$(firstletter),$(INPUT)) thirdletter := $(call nextletter,$(firstletter)$(secondletter),$(INPUT)) 

and etc.

It is ugly, but it is not agnostic.

0
source

Source: https://habr.com/ru/post/927242/


All Articles