0

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
2

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 agree to our terms of service, privacy policy and cookie policy

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