xargs Into cd Link to heading
Since xargs
runs in a subprocess, you can’t echo mydir | xargs cd
and propagate it to the parent shell.
In order to achieve something like this you have to use another means of executing code over the result:
# the obvious way, if you are writing a script
mydir="$(ls ~/myfolder | grep mydir)"
cd "$mydir"
# as a one liner
ls ~/myfolder | grep mydir | while read -r result; do cd "$result"; done
This is useful for doing something like this:
# 1. find all dirs in cwd
# 2. interactively choose on through fzf
# 3. cd into it
fd --type=d . | fzf --prompt "cd into: "| while read -r result; do cd "$result" || exit; done
That’s cool and all Link to heading
But fzf has a builtin utility for this:
<Alt+c>
: opens an interactive prompt tocd
into selected dir
Links Link to heading