How do I redirect output to cd?
Is it possible to redirect a command’s output to
cd? For example, I searched for a directory using
locate and got the path to it. Now, instead of writing a
cd path, can I redirect the
locate output to
I tried this:
$ locate Descargas | grep Descargas$ /home/oliver/Descargas $ locate Descargas | grep Descargas$ | cd $ locate Descargas | grep Descargas$ > cd $ locate Descargas | grep Descargas$ < cd /home/oliver/Descargas $
No luck. This probably isn’t particularly useful, but I’m curious.
You want command substitution, not redirection:
cd "$(locate Descargas | grep -F 'Descargas$')"
The bits between the
$( and the
) are run as a command and the output (stripped of any final newline) is substituted into the overall command.
This can also be done with ‘back ticks’ (“`”):
cd "`locate Descargas | grep -F 'Descargas$'`"
The dollar-paren syntax is generally preferred because it is easier to deal with in nested situations:
# contrived cd "$(grep '^dir: ' "$(locate interesting-places | head -1)" | sed 's/^[^ ]*//')"