Olivier Refalo
Olivier Refalo

Reputation: 51435

Can a Fish script tell what directory it's stored in?

So, I really like Fish - but I need some help with scripting. and in particular finding the path of the script being run.

Here is the solution for BASH Getting the source directory of a Bash script from within

Can anyone help me find the equivalent with fish?

Upvotes: 20

Views: 6550

Answers (6)

himnabil
himnabil

Reputation: 378

dirname (status --current-file) didn't work for me so I tried basename (pwd) and it works

Upvotes: 0

Sir-Photch
Sir-Photch

Reputation: 11

status dirname

will work in newer versions of fish.

Upvotes: 1

Marek Fiala
Marek Fiala

Reputation: 11

Since readlink -m does not exist on macOS,

DIR=dirname (realpath (status -f))

Works on macOS and Linux at the same time.
This solution also has the benefit to work from an .sh file or even the command line.

Upvotes: 1

user3488060
user3488060

Reputation:

In fish shell,

set DIR (cd (dirname (status -f)); and pwd) 

is an equivalent to the BASH one liner

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

mentioned in Can a Bash script tell what directory it's stored in?.

NOTE: fish shell will cd into that dir and stay there. Bash will cd but it stays contained in subcommand.

Upvotes: 11

Limeth
Limeth

Reputation: 513

File path

To get the full path of the script, use status --current-filename, or set FILE (status --current-filename) to declare a variable.

Directory path

To get the full path to the directory the script is stored in, use dirname (status --current-filename), or set DIR (dirname (status --current-filename)) to declare a variable.

The equivalent to this question is the following:

set DIR (dirname (status --current-filename)) is the equivalent

Upvotes: 5

ridiculous_fish
ridiculous_fish

Reputation: 18551

status --current-filename will output the path to the currently executing script.

For more information on the status command, you can run man status or see the documentation at http://fishshell.com/docs/current/commands.html#status

Upvotes: 28

Related Questions