I have a script that is trying to copy a directory from a path that includes backslash-escaped spaces and is stored in a string variable to a subdirectory of the working directory.When I run the script, I get the usage for cp:usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file       cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directoryHowever, if I echo the command, it looks perfectly correct. And even if I then copy and paste that echoed line back into the terminal, it executes fine!rm -rf Payloadmkdir Payloadecho cp -Rf $APP_PROTOTYPE/ Payload/${BUNDLE_NAME}.appcp -Rf $APP_PROTOTYPE/ Payload/${BUNDLE_NAME}.appI'm at a loss to understand why it would not execute properly in Bash, but manually pasting the command back in works flawlessly. Moreover I can't even tell what the problem is because it works fine when I do it manually (however I obviously suspect the spaces). The exit code from cp is 64, if that helps at all.$APP_PROTOTYPE looks like .golden_repo/Production\ Releases\ Only/Kiosk/1.0.3/Prototype/Kiosk.app, and note that .golden_repo is a mounted smbfs share.
Trying to cp a path with spaces, fails when scripted but echoing and pasting succeeds?
bash;osx;cp
You need to quote parts of your command containing spaces to prevent word splitting from making them into two different arguments:cp -Rf $APP_PROTOTYPE/ Payload/${BUNDLE_NAME}.appshould do what you want. Note that you need double quotes rather than single quotes to allow for variable interpolation inside the quotes.The output of echo doesn't mean that the command should work properly. Unlike cp, which deals in path names, echo is perfectly fine with one argument becoming several ones due to word splitting:echo 'a b'andecho a bshould both produce the same result.