Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the path of the current source file with fish

Tags:

fish

In a fish shell script, is it possible to obtain the path where the shell script lives? In bash, you can use the BASH_SOURCE variable to do this.

like image 470
Lorin Hochstein Avatar asked Jan 10 '10 15:01

Lorin Hochstein


1 Answers

One way is to use the status command.

eg.

~/tmp> echo status -f > show_filename.fish
~/tmp> fish show_filename.fish
/home/foo/tmp/show_filename.fish

--

Ah, sorry - judging by your comment, I misread the question. I thought you meant the fully qualified path, instead of the script's directory.

You could just send the status result to dirname. It's not a built-in function of fish, like status is, but fish tends to outsource as much of this functionality as it can anyway (see "The law of minimalism", over in the fish design doc). I can't see anything better commands in the docs for this, so maybe this is as good as we can get for now?

Amending the example, change the contents of show_filename.fish to be:

dirname (status -f)

Now you should get:

~/tmp> fish show_filename.fish
/home/foo/tmp
like image 176
otherchirps Avatar answered Sep 30 '22 19:09

otherchirps