How to return the first character of a variable in gmake

Using GNU make, I would like to extract the first character of a variable. I am currently using a shell function to make bash execute a substring. I want to know if there is a way to use gmake built-in modules to do the same.

 DIR=/user/$(shell echo "$${USER:0:1}")/$(USER)/ 
+4
source share
2 answers

This is not very nice, and you will need to add to $(INITIALS) until you are happy, but:

 INITIALS := abcdefghijklmnopqrstu vwxyz U := $(strip $(foreach a,$(INITIALS),$(if $(USER:$a%=),,$a))) DIR = /user/$(U)/$(USER)/ 

Perhaps a sensible approach would be to take into account the use of := in the above and change your simple version to DIR := ...$(shell ...)... so that the shell command is called only once.

+4
source

http://www.gnu.org/software/make/manual/make.html#Functions is a complete list of everything you can do with the built-in built-in gmake.

It seems that it is not possible to extract the first character without $(shell) , unfortunately.

+1
source

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


All Articles