#!/bin/bash

# Command substitution is a useful and powerful way to use the output of a
# command as a value of a variable.
# Syntax: $(command)
# The command is run and anything it writes on stdout is returned as a
# value.

CURRENTDIR=$(pwd)
echo ${CURRENTDIR}

MYHOMEFILES=$(ls $HOME)
echo ${MYHOMEFILES}

# You can read from a file (with any empty or trailing lines removed).
# Very useful!

filename="testfile.txt"
echo "1 2 3" >> "$filename"
echo "a b c" >> "$filename"
echo "" >> "$filename"
echo "" >> "$filename"
echo "Contents of ${filename}:"
cat $filename
FILECONTENTS=$(< $filename)
echo "Contents of the variable:"
echo $FILECONTENTS

# Warning: command substitution runs in a subshell!
# This can generate some unexpected results if you are not aware of it.
#
# Write an example that demonstrates it.
