bash print variable code example

Example 1: save output of command to craible bash

password=$(openssl rand -base64 32)
echo $password

Example 2: 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 3: echo variable bash

echo "${var}"

Example 4: bash echo 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

Example 5: bash create variable

#create a variable to store name
name="Lance Armah"

Example 6: print variable in bash

#!/bin/bash  
echo  
echo "When single quote is used with string:"  
invitation='Welcome to javatpoint'  
echo $invitation  
echo  
echo "When double quote is used with string:"  
invitation="Welcome to javatpoint"  
echo $invitation  
echo  
echo "When variable is used with double quote:"  
Remark="Hello User!, $invitation"  
echo $Remark  
echo  
echo "When variable is used with single quote:"  
Remark='Hello User!, $invitation'  
echo $Remark  
echo

Tags:

Php Example