Expanding a directory variable

A variable can store a directory path:

dupdir=mydata/test/dup.marked
mkdir "$dupdir/new_folder"

Use braces when text next to the variable name could otherwise be interpreted as part of its name. $dupdir and ${dupdir} expand the same value; the braces specify where the variable name ends. The original path-construction example combined a directory with a derived name:

mkdir "$dupdir/${dupdir}_new/new_folder"

Here ${dupdir}_new means the value of dupdir followed by _new. Because dupdir contains a path, that value introduces directory separators into the result; choose the value according to the intended directory structure.

Expanding a filename

Double quotes preserve spaces and prevent an expanded filename from being split into separate arguments. The recorded filename example was:

base="MY_DATA"
rm "${dupdir}.shift/${base}.bam"

Braces separate the directory variable from the .shift suffix. Quoting the complete path keeps the expanded filename together.

← Back to all posts