Code: -

filesDirName="/C/Users/OM\\ SAI\\ RAM/HelloWorldSagar"
echo ${filesDirName}
echo "cd ${filesDirName}"
cd ${filesDirName}

bash output: -

$ ./files.sh
/C/Users/OM\ SAI\ RAM/HelloWorldSagar
cd /C/Users/OM\ SAI\ RAM/HelloWorldSagar
./files.sh: line 4: cd: too many arguments

Like this:

# No backslash before space needed, as the entire sting is quoted with "
filesDirName="/C/Users/OM SAI RAM/HelloWorldSagar"
# echo allows multiple parameter, so it worked by chance
# You should quote it anyway
echo "${filesDirName}"
echo "cd ${filesDirName}"
# quote the parameter
cd "${filesDirName}"

Rule of thumb: If in doubt, quote the variable.

BTW: Indent your code by 4 spaces, so it is displayed as a code block

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.