Tiki Tiki - 2 years ago 91
Bash Question

Change directory in fish function and return to original directory after abort

I am switching from bash to fish, but am having trouble porting over a convenience function I use often. The point of this function is to run make from the root directory of my source tree regardless of which directory my shell is currently in.

In bash, this was simply:

function omake {(
make $@;

Since fish doesn't have subshells, the best I've been able to do is:

function omake
make $argv

This works, but with the caveat that after interrupting the fish version with ^C, the shell is still in $SOURCE_ROOT, but interrupting the bash version puts me back in the original directory.

Is there a way to write a script that works identically to the bash one in fish?

Answer Source

This is as close as I can get to a subshell:

function omake
    echo "cd $SOURCE_ROOT; and make \$argv" | fish /dev/stdin $argv

Process substitution does not seem to be interruptable: Ctrl-C does not stop this sleep cmd

echo (cd /tmp; and sleep 15)

However, fish has a very nice way to find the pid of a backgrounded process:

function omake
    pushd dir1
    make $argv &

Then, to stop the make, instead of Ctrl-C, do kill %make

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download