I am attempting to change directory within a script in order to perform a bunch of operations using relative paths. The folder is a variable named $input_path:
cd $(echo "$input_path")
If there is a space in the variable, for example "/home/user/test directory/subfolder", the script returns an error:
./test_currently_broken.sh: line 86: cd: "/home/user/test: No such file or directory
I have tried various ways to escape the spaces:
# escape spaces using backslashes, using sed
input_path=$(echo "$input_path" | sed 's/ /\\ /g')
or
# wrap input path with awk to add quotes
input_path=$(echo "$input_path" | awk '{print "\"" $0 "\""}')
or
# wrap in single quotes using sed
input_path=$(echo "$input_path" | sed -e "s/\(.*\)/'\1'/")#
But none fix the error - it still fails to change directory.
I have confirmed that the directory it is attempting to change to definitely exists, and cding works outside of this script.
Is there a solution to this strange behaviour of cd?