Score:1

Making folders when the names have "/" and " ' "

in flag
Cas

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)'"
Score:10
cn flag

Single or double quotes will not cause any problems as long as you keep the variables properly quoted at all times.

$ title=$(cat << END_TITLE
This "is a title" with 'two kinds of quotes'
END_TITLE
)

$ declare -p title
declare -- title="This \"is a title\" with 'two kinds of quotes'"

$ mkdir "$title"      # no need to try to force extra quotes in here

$ echo $?
0

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 -:

mkdir "$root_folder/${title//\//-} ($year)"

That uses bash's Shell Parameter Expansion instead of calling out to sed.

Jeff Schaller avatar
ru flag
Another option could be to fake it with a unicode [fraction slash U+2044](https://www.compart.com/en/unicode/U+2044) or [division slash U+2215](https://www.compart.com/en/unicode/U+2215)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.