Adam
Adam

Reputation: 1744

How to retrieve the current script's path in Fish shell

In bash, we can retrieve the current script's path by $0 variable, so if any script has a dependency resource which is under the same directory as the script directory, we can use it even when we're not executing the script in the script's directory.

How can I retrieve the current script's path in fish shell?

Upvotes: 10

Views: 1980

Answers (2)

Pothi Kalimuthu
Pothi Kalimuthu

Reputation: 321

The other (right & selected) answer was too old (nearly a decade old). As of 2024, with fish version 3.7.1, the (simplified) answer/s to the original question is...

Answer #1

PWD

Yes, that's all you need. For example, if the script has echo $(PWD), then the output would show the actual script's path. PWD is basically an environment variable that fish sets. If you type set command within a fish shell or script, it'd display all the environmental variables (including PWD).

Answer #2

This is the shortened version of the original / selected answer...

realpath (status dirname)

status dirname would show the current directory that is usually the relative path. The fish-builtin realpath displays the absolute path, following any symlinks.

Upvotes: 0

ridiculous_fish
ridiculous_fish

Reputation: 18551

status --current-filename is what you're looking for.

Note that this handles both sourced and executed files, while $0 in bash is only for executed files.

Upvotes: 15

Related Questions