1

I want to move a file from a directory different from the current directory. This is the solution I thought of:

mv (cd ~/Downloads; ls -t | head -1 | xargs -I {} readlink -f {}) ./

There is probably a better way, but along the way I found that my expectation of the change of directory staying inside the subcommand was wrong: Running cd changes the directory where mv is being executed.

So, is there a way to change directories only for the current subcommand, without affecting the top command?

honestSalami
  • 247
  • 1
  • 6
  • Sort of borderline, since it's *kind of* about "shell scripting", but I'd really recommend this type of "general `fish` use" question for [Unix and Linux Stack](https://unix.stackexchange.com) instead of Stack Overflow (which is for "programming questions"). Just be aware in case you get downvotes, that others may not feel it is quite as "borderline". – NotTheDr01ds Apr 29 '21 at 18:32
  • Looks firmly on topic to me – that other guy Apr 29 '21 at 18:51

1 Answers1

6

Right, many of us coming from Posix shells like bash or zsh are used to being able to run a command substitution like this using $() or just backticks and leave the parent shell environment untouched. It's a nice trick, IMHO.

On the other hand, fish doesn't automatically create a subshell when using its command substitution operator (). There's a feature request here for that, but the workaround (as suggested there) is fairly straightforward -- Just explicitly create a subshell inside the command substitution. E.g.:

mv (fish -c 'cd ~/Downloads; ls -t | head -1 | xargs -I {} readlink -f {}') ./

The downside is that syntax highlighting/checking doesn't work in the quoted text, and quoting/escaping rules get more complicated.

NotTheDr01ds
  • 15,620
  • 5
  • 44
  • 70