brianegge
brianegge

Reputation: 29892

How to return the first character of a variable in gmake

Using GNU's make, I'd like to extract the first character of a variable. Currently I'm using the shell function to have bash perform the substring. I'm wanting to know if there is a way using gmake's built-ins to do the same.

DIR=/user/$(shell echo "$${USER:0:1}")/$(USER)/

Upvotes: 4

Views: 1749

Answers (2)

John Marshall
John Marshall

Reputation: 7005

It's not very satisfying, and you'd have to add to $(INITIALS) until you were happy, but:

INITIALS := a b c d e f g h i j k l m n o p q r s t u v w x y z
U := $(strip $(foreach a,$(INITIALS),$(if $(USER:$a%=),,$a)))

DIR = /user/$(U)/$(USER)/

Perhaps the sensible approach would be to take note of the := usages in the above, and amend your simple version to DIR := ...$(shell ...)... so that the shell command is only invoked once.

Upvotes: 4

zwol
zwol

Reputation: 140758

http://www.gnu.org/software/make/manual/make.html#Functions is a comprehensive list of everything you can do with gmake builtins.

It does not appear to be possible to extract the first character without $(shell), unfortunately.

Upvotes: 2

Related Questions