-h, –no-filename : Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.
-o, –only-matching : Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Also, we wrapped out pattern with the \w* that matches all word-constituent characters on either side. The * character states that it should find 0 or more of those characters in the pattern to match.

A variable in bash (and any POSIX-compatible shell) will be in one of the three following states:

unset

set but empty

set and not empty

Variables in bash do not have data types. A variable can contain a number, a character, a string of characters or the empty string. Assigning a value to the reference of a variable will create it, you do not need to declare it beforehand.

Following, you will find some tests that allow you to find which state the variable is in. They will allow you to distinguish between ‘unset’ and ‘set but empty’ states.

# Test if VAR is 'unset' or 'set but empty'
if [ -z "${VAR}" ]; then
echo "VAR is 'unset' or 'set but empty'";
fi
# Test if VAR is 'unset'
if [ -z "${VAR+BFNET}" ]; then
echo "VAR is 'unset'";
fi
# Where ${VAR+BFNET} is a parameter expansion which evaluates to null if VAR is 'unset',
# if VAR is set to anything (including the empty string) the construct expands to BFNET.
# Test if VAR is 'set but empty'
if [ -z "${VAR-BFNET}" ]; then
echo "VAR is 'set but empty'";
fi
# Where ${VAR-BFNET} is a parameter expansion which evaluates to the value of VAR if VAR is 'set' (either empty or not),
# if VAR 'unset' the construct expands to BFNET.
# This is useful for providing default values
# For example:
#function custom_echo () {
# local DEFAULT_MSG="No message passed."; # Doesn't really need to be a local variable.
# MESSAGE=${1:-$DEFAULT_MSG}; # Defaults to default message.
# echo -e "$MESSAGE";
# return;
#}
# In the above function if the user does not provide the message to be printed with the function call, then the default message will be used.
# Test if VAR is 'set and not empty'
if [ -n "${VAR}" ]; then
echo "VAR is 'set and not empty'";
fi