46

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

echo "/some/directory/path" | xargs -n1 cd
gparyani
  • 1,845
  • 9
  • 30
  • 48
Ian Lotinsky
  • 575
  • 1
  • 4
  • 6

2 Answers2

39

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.

Ignacio Vazquez-Abrams
  • 111,361
  • 10
  • 201
  • 247
  • That makes complete sense. Thanks for helping a Unix noob. – Ian Lotinsky Nov 03 '10 at 19:36
  • 4
    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 03 '10 at 20:27
  • 8
    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 03 '10 at 20:36
  • /usr/bin/cd is definitely a Unix free standing documented command, at least on Solaris. – jlliagre Nov 03 '10 at 23:31
  • Using @Slartibartfast for auto-generated paths: ``cd `port file libcudd | sed -e 's/\/Portfile//'` `` (usually long ones you don't want to handle manually, like this MacPorts Portfile location). – 0 _ Dec 05 '13 at 10:22
  • I got here because I wanted to change the path before cd-ing into it in the current shell, and found that I can do that using a bash function: https://stackoverflow.com/a/7131683/1386750 – AstroFloyd Dec 11 '21 at 07:26
33

The command cd is a built-in because the information about the current directory is tied to a process and only shell built-in can change current directory of the running shell.

There are two problems with your code:

  1. xargs cannot run cd because cd is a built-in command and xargs can run only executable files.
  2. Even if you run cd in a sub-process called from xargs, it will not have any effect on the parent process as explained above.

The solution is to run a sub-shell, inside it run cd and then you can execute commands in the new current directory.

ls | xargs -L 1 bash -c 'cd "$0" && pwd && ls'
parpa
  • 331
  • 3
  • 2