My bash script registers its own path so it can return to its own folder after doing a cd
somewhere (kinda if it explores a different forest and is able to relocate its house).
And if the path have spaces, I've used sed
to add \
before every space.
But when using cd $SCRIPTPATH
to this variable:
./newsed.sh: 11: cd: can't cd to /media/daniell/B/bkp/ST500LM012\
Source: newsed.sh:
#!/bin/bash -e
tmp="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
SCRIPTPATH="$(echo "$tmp" | sed 's/ /\\ /g')"
echo "$SCRIPTPATH"
ls
cd
ls
cd $SCRIPTPATH
ls
echo $SCRIPTPATH
produces a normal path (/media/daniell/B/bkp/ST500LM012\ HN-M500MBB\ Dat/0/ok/Pendrive/flshdrive/0.Floflis-DNA/layers/soil
) that I can manually copy and cd into it, and works; but when cd
'ing directly to the variable (cd $SCRIPTPATH
) it crashes right after the \
.
Use quotes around variables to prevent spaces from being treated as delimiters in the shell, i.e.
will work.