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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
In POSIX shells:
cd -P -- "$(mycommand)"
$(cmd)is command substitution. It expands to the standard output of
cmdminus 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
mycommandwon't start with
+, you can omit it. If not
thatoutput my be taken as option to
cdis the command to change the current directory. However, without
-P, it might not change to the directory given as argument if that contains
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
Now I hear you ask, how would I change in a directory that ends in newline characters with those commands?
For simple cases:
cd `my command`
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.