Scott Whittaker

Software engineer

Day job React | side projects Svelte

Assign the result of a command to a variable in a bash script

I do not write bash scripts anywhere near often enough to recall the basics so I inevitably lose time searching for answers. In this case I failed to understand how to assign the result of a command to a variable.

All I wanted to do was assign a random number to a variable like so…


RANDOM_NUMBER=jot -r 1 2000 65000

This results in the following output when you run this script…

$ ./
./ line 3: -r: command not found

To assign the result of the command to the variable RANDOM_NUMBER there are 2 approaches…

Wrap the command in $()


RANDOM_NUMBER=$(jot -r 1 2000 65000)

Wrap the command in backticks


RANDOM_NUMBER=`jot -r 1 2000 65000`

Everyday I feel like a beginner. I found the answer here should you want to explore further.