I'm building up file paths (and mkdir
-ing them) based on variables:
mkdir "$root_folder/$title ($year)"
I came across a situation where $title was 9/11: Inside the President's War Room
. The /
is a problem because mkdir will interpret it as a folder path. So I want to replace all /
with \/
so that mkdir takes it literally instead of thinking it's a path. The '
in the name is also giving problems. So I also want to replace all '
with \'
but this is harder than I thought.
My end goal is to just make the folders in such way that those /
and '
are literally interpreted. Some things I've already tried:
title=$(echo "$title" | sed -e "s|'|$(echo "\\")\'|g")
${title/\//\\/}
#---
title=$(echo "$title" | sed -e "s|'|$(echo "\\")\'|g" -e "s|/|\\/|g")
$title
#---
mkdir "$root_folder/'$title ($year)'"
Single or double quotes will not cause any problems as long as you keep the variables properly quoted at all times.
filenames may not contain a / character, no matter how hard you try to escape it. Slash is the directory separator. You'll have to substitute a different character. Suppose you choose -:
That uses bash's Shell Parameter Expansion instead of calling out to sed.