Linux OS

Q1: How can I check if a variable is empty in bash?

In bash at least the following command tests if $var is empty:

if [[ -z "$var" ]]

The command man test is your friend.

Presuming bash:

var=""

if [[ -z "$var" ]]; then
 echo "not empty"
 else
 echo "empty"
 fi

Q2: How to assign a shell command output to a variable?

var=$(command-name-here)
var=$(command-name-here arg1)
var=$(/path/to/command)
var=$(/path/to/command arg1 arg2)

OR

var=`command-name-here`
var=`command-name-here arg1`
var=`/path/to/command`
var=`/path/to/command arg1 arg2`
Advertisements