I have a bash beginner problem:
My path to be created is /Volumes/ADATA\ UFD/Programming/Qt, where /Volumes/ADATA\ UFD exists already. I’d like to write a script in the following form:
# create a single output directory
outputdir="/Volumes/ADATA\ UFD/Programming/Qt"
mkdir -pv $outputdir
My problem is that mkdir creates the directory /Volumes/ADATA and ./UFD/Programming instead of creating /Volumes/ADATA\ UFD/Programming/Qt.
I have looked at this question on SO; however, none of these solutions worked:
outputdir=/Volumes/"ADATA\ UFD/Programming/Qt"
mkdir -pv $outputdir
outputdir=/Volumes/'ADATA\ UFD/Programming/Qt'
mkdir -pv $outputdir
outputdir='/Volumes/ADATA\ UFD/Programming/Qt'
mkdir -pv $outputdir
outputdir=/Volumes/ADATA' 'UFD/Programming/Qt
mkdir -pv $outputdir
What am I doing wrong? What is the good combination here?
You need to quote the variables when you use them. Expanded variables undergo wordsplitting. It’s good practice to always quote your expansion, regardless of whether or not you expect it to contain special characters or spaces. You also do not need to escape spaces when quoting.
The following will do what you want: