I was hoping I could just do cd | [my command] but it seems cd doesn't work from a pipe.

I want to simply cd into the output of [my command] without writing a bash script of sorts I'm hoping there is a simple way of doing this.


In POSIX shells:

cd -P -- "$(mycommand)"
  • $(cmd) is command substitution. It expands to the standard output of cmd minus the trailing newline characters.
  • "$(cmd)". Without the quotes that expansion would be subject to split+glob which we don't want here.
  • -- separates options from arguments. If you know that the output of mycommand won't start with - nor +, you can omit it. If not that output my be taken as option to cd
  • cd is the command to change the current directory. However, without -P, it might not change to the directory given as argument if that contains .. components.

Or you could write cd $(mycommand). That would be very sloppy, but that would work provided the output of mycommand does not contain spaces, tabs, newline, *, ?, [ characters or .. components and doesn't start with - or +.

Now I hear you ask, how would I change in a directory that ends in newline characters with those commands?

  • 1
    Whether -P is necessary really depends on what the path was intended to mean. cd -P could just as well end up in an unintended place (or an error) as bare cd. – Michael Homer May 12 '17 at 22:43
  • @MichaelHomer, The way cd without -P handles a path is unique to the cd builtin of POSIX shells, not to any other command. Unless that command was especially designed to output a path meant to be used by the cd builtin of a POSIX shell (not of any other shell, not by any other command), then it should be treated as a normal path, so -P should be used. Now, granted, it's a corner case. – Stéphane Chazelas May 12 '17 at 22:47
  • I don't share your faith that everything generating a/b/.. paths checks whether b is a symlink before giving it out. – Michael Homer May 12 '17 at 22:54
  • @MichaelHomer, it's a fact that /a/b/.. as a path to any system call and to any command (except the cd builtin of some shells) is the parent directory of /a/b. If you do ls -l /a/b/.., you'll list the content of that directory. If you call chdir() on that in any language, that will take you to that directory. If you call cd (in POSIX shells) on it without -P, that may or may not take you to that directory. cd -P will. If something outputs a/b/.. meaning it to be /a, then it can only be used by those cd without -P (note that ls -l or the chdir() of any language). – Stéphane Chazelas May 12 '17 at 23:03
  • Of course, one may choose to ignore the problem which in effect is unlikely to happen just as I ignored (but hinted) to the problem of directories ending in newlines (or paths with invalid characters with yash, or a directory called -). – Stéphane Chazelas May 12 '17 at 23:06

For simple cases:

cd `my command`

where my command is enclosed in back ticks.

This doesn't work if the output of the command contains “weird” characters. See Stéphane Chazelas's answer for more details.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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