bash how to assign output to variable


Backquotes (``) are used for a command substitution.


Command "date" return date to variable "var". Alternative method to use command substitution is "$()":

var =$(date)

If we want to get value from "var" use "$var":

echo "$var"

Output: Tue Jul 12 15:33:06 CEST 2016

It's properly always use double quotes around variable substitution. Without double quotes, the variable is expanded which may cause problems if variable contains special characters.

Make sure there is no space between variable name before and after assign char (=). You can assign output from pipeline to variable:

NUM_FILES=`ls | wc -l`

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.