PDA

View Full Version : [SOLVED] simple bash question



glederfein
June 14th, 2010, 05:14 PM
I want to write a bash script that makes a directory in a path with spaces in it.
For example:



#!/bin/bash
dir="/cygdrive/c/Documents and Settings/guy/My Documents"
mkdir $dir/new


For some reason this splits up the $dir variable (by spaces) and mkdir fails.

Please help.

Bachstelze
June 14th, 2010, 05:22 PM
You must wrap it in quotes. Also, there's no reason to use /bin/bash for something that simple, just use /bin/sh:


#!/bin/sh
dir="/cygdrive/c/Documents and Settings/guy/My Documents"
mkdir "$dir/new"

MadCow108
June 14th, 2010, 05:42 PM
also mkdir fails when the parent does not exist.
solution: add -p flag so it creates the parent too.