Tell me more ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Feeling like an idiot right now. Why does this not work?

echo "/some/directory/path" | xargs -n1 cd
share|improve this question

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
That makes complete sense. Thanks for helping a Unix noob. – Ian Lotinsky Nov 3 '10 at 19:36
You can get the effect you want by using back-quotes: cd `echo "/some/directory/path" | cut -d\ -f1` (Note that I added 'cut' to split on spaces and grab the first item the way xargs does) – Slartibartfast Nov 3 '10 at 20:27
1  
Actually, xargs can't run cd since it's, of necessity, a shell builtin and xargs can only run free-standing executables. What you said is true about subprocesses, however. – Dennis Williamson Nov 3 '10 at 20:36
/usr/bin/cd is definitely a Unix free standing documented command, at least on Solaris. – jlliagre Nov 3 '10 at 23:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.