How can I get the bash $RANDOM in a makefile?

This works on OSX:

RANDOM := $(shell /bin/bash -c "echo $$RANDOM")
test: echo $(RANDOM)

For cross platform random numbers I've instead resorted to this:

RANDOM := $(shell od -An -N2 -i /dev/random | tr -d ' ')

How can I get the first example to work?

1 Answer

The reason your first line works on OSX and not Ubuntu, is likely because sh is bash on OSX. $$RANDOM is already expanded by the time the bash -c gets around to executing it, since you used "" quotes. So you probably want this:

RANDOM := $(shell bash -c 'echo $$RANDOM')

Personally I'd probably go with awk. Something like

awk 'BEGIN{srand();printf("%d", 65536*rand())}'

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like