save output bash to variable code example

Example 1: store printed output in variable bash

#For storing and printing output at the same time do following:
OUTPUT=$(ls -1) #First assign output of command execution to a variable 
echo "${OUTPUT}" #Then print out the result in variable into the terminal

Example 2: save output of command to variable bash

OUTPUT="$(ls -1)"
echo "${OUTPUT}"

MULTILINE=$(ls \
   -1)
echo "${MULTILINE}"

Example 3: bash store script output in variable

#Just use following structure to store output of command into a variable:
var=$(command)
#For example:
var=$(echo 'hi')	#store hi into var
var=$(ls)	#store list of files into var