I’m trying to create a function that goes to a specific directory, and if a single argument is specified add that to the directory string. But no matter what I try I can’t get it to work and bash always reports that X is not a directory even though if I copy/paste it to a new command line it works from there.
function projects ()
{
DIR=
if [ -n "$1" ]
then
DIR=$1
fi
echo "~/Projects/Working Branches/$DIR" | sed 's/ /\\&/g' | xargs cd
}
–
$ projects Parametric\ Search\ API
/usr/bin/cd: line 4: cd: ~/Projects/Working Branches/Parametric Search API: No such file or directory
$ projects "Parametric Search API"
/usr/bin/cd: line 4: cd: ~/Projects/Working Branches/Parametric Search API: No such file or directory
I’ve also tried modifying the sed regexp to s/ /\\\\\\&/g which produces what appears to be a properly escaped string, but results in the same exact error
$ projects Parametric\ Search\ API
/usr/bin/cd: line 4: cd: ~/Projects/Working\ Branches/Parametric\ Search\ API: No such file or directory
$ projects "Parametric Search API"
/usr/bin/cd: line 4: cd: ~/Projects/Working\ Branches/Parametric\ Search\ API: No such file or directory
An finally I’ve tried surrounding $DIR with every combination of quotes I can think of, but no luck so far.
So, one problem is that if you quote a path with
~in it, the shell will not expand that before passing it intocd. You can fix this by using$HOMEinstead, or by putting the~/outside of the quotes.Also, you don’t need to conditionally set
$DIRbased on$1, nor do you need to usesedandxargs. Just use$1directly in your path; if it’s empty, that directory component will be empty, leaving you with just theWorking Branchesdirectory as an argument tocd.Simply
should work, as would the slightly shorter but less clear