using variables when copying files

smaller sample from a larger script: i'm simply trying to copy files from one desktop directory on a mac to another using variables. when i first test the logic of the script without variables, everything works and the copy is effective.

but i'm unable to call the same copy by passing a variable. script runs as root, FYI. much obliged if you can weigh in. hoping it's just a syntax error that i'm missing.

#!/bin/sh

oldWS="/Volumes/Lion\ BaseOS/Users/workstation"
newWS="/Users/workstation"

echo "Migrating any old Desktop files from $oldWS/Desktop..."
cp "${oldWS}/Desktop/*" "${newWS}/Desktop"
if [ $? != 0 ]; then
	echo "There was a problem migrating files from $oldWS/Desktop, PLEASE INVESTIGATE."
else
	echo "Files from $oldWS/Desktop have been moved to the new Mac."
fi

exit 0

i'm assuming that i've got a syntax error here but i can't see where. i use similar syntax in other scripts all the time...

thanks for whatever insight you might have to share.

david

---------- Post updated at 03:27 PM ---------- Previous update was at 03:20 PM ----------

forgot to mention the error i'm getting:

cp: /Volumes/Lion\ BaseOS/Users/workstation/Desktop/*: No such file or directory

which is odd as:
a) the directory does exist
b) when i run the cp manually and without variables, it's. just. fine.

  • does not expand inside double quotes. Put it outside the quotes.

"string"*

Also, you don't need to do if [ $? .. since you can fit the cp directly into the if itself.

if ! cp "${oldWS}/Desktop/"* "${newWS}/Desktop"
then
...
else
...
fi

Also, you can use cp's -R option to avoid needing to use * at all.

Also, you don't need to escape spaces in paths that are inside double-quotes.

oldWS="/Volumes/Lion BaseOS/Users/workstation"

three answers in one:

a) didn't know wildcards couldn't be called in quotes.
b) the reminder about my backslash.
c) including the cp into the if statement.

way more elegant now. thank you!!