------------------------------------------------------------------------
command substitution
------------------------------------------------------------------------

command substitution allows you to capture the output of a command.
Usually this is useful in using the outut of a command as an input parameter for another command. Command substitutions are run in subshells. Command substitutions are run in subshells. Command substitutions are run in subshells. Command substitutions are run in subshells.


$ wc -l < jq.txt
224

$ nlines=$(wc -l < jq.txt)     #new style
$ echo $nlines
224

$ file="jq.txt"
$ echo "file $file has $(wc -l < $file) lines"  #pretty neat

Equivalently
$ nlines=`wc -l < jq.txt`   #old style

Command substitutions can be nested!

However, command substitution eats up \n and \t

$ echo "Hello\t\tKitty"
hello		Kitty

$ echo $(echo "Hello\t\tKitty")
Hello Kitty                          #\t eaten up

$ seq 3
1
2
3

$ echo $(seq 3)
1 2 3				#\n eaten up

This "feature" is actually helpful. Consider for example the following
$ cat list_of_files.txt
a.txt
b.txt
c.txt

Now you want to delete all files listed in list_of_files.txt

$ rm $(cat list_of_files.txt)

will delete all files in this file (even though the filenames are on separate
lines).

If you want to retain \t, \n and so on you must quote.

$ cat list.txt
a
b
c

$ echo $(cat list.txt)          #\n gobbled up
a b c

$ echo "$(cat.list.txt)"        #\n not gobbled up
a
b
c