In shell scripts it is common to set variables using the output of a command, e.g.


The output from the command is normally a single value. If, however, the command returns multiple values you need to use some other program (e.g. awk or sed) to split the combined result into separate parts. Alternatively, you could pass an argument to the command to specify which result should be returned.

A neater solution is to use the shell's eval command. For example, say you wanted to return day, month and year from the date command. You'd have to write either:

day=`date +%d` 
month=`date +%m` 
year=`date +%Y`


result=`date '+%d %m %Y'`

and then break up $result with awk or cut for example. Instead you can write:

eval `date '+day=%d month=%m year=%Y'`

This will set $day, $month and $year as separate variables that can then be used later in the script.

Recent Changes

Contribute to this wiki

Why not help others by sharing your knowledge? Contribute something to this wiki and join out hall of fame!
Contact us for a user name and password