Feeling like an idiot right now. Why does this not work?
echo "/some/directory/path" | xargs -n1 cd
The pipe runs xargs in a subprocess, and xargs runs cd in a subprocess. Changes in a subprocess do not get propagated to the parent process.
There are two problems with your code:
The solution is to run a sub-shell, inside it run