空白のあるフォルダーを渡す際にエラーが発生しました。

空白のあるフォルダーを渡す際にエラーが発生しました。

パスワード:-

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

排出出力:-

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

ベストアンサー1

このように:

# 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}"

経験則:疑わしい場合は変数を引用してください。

注:コードを4行インデントしてコードブロックとして表示します。

おすすめ記事