Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This works on OSX:

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

    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?

share|improve this question

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())}'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.