Kevin P.
Kevin P.

Reputation: 31

How to make a fish script executable in any directory?

I made the switch to Ubuntu a couple of months ago and still do not have very much experience with unix and fish aside from the basic/often used commands. For a C class that I am taking I constantly need to SSH onto a remote linux machine run by my university called Zeus. The command to ssh onto it is a bit lengthy, and I would like to be able to make the process faster with a fish script.

I wrote a file called "zeus.fish" that essentially just calls my ssh command, among a few other things. Currently I can only run it by calling "fish zeus.fish" within the directory of the file. I would like to be able to run the script from within any directory, just like any other command. IE just typing "zeus" would attempt to log me into the remote machine. I'm assuming that I have to do something with PATH. How can I do this? Thanks!

Upvotes: 3

Views: 5776

Answers (3)

tolgraven
tolgraven

Reputation: 563

ridiculous_fish's answer is (obviously) entirely correct, but actually complicating it a bit in my opinion.

funced zeus; and funcsave zeus

will do the same thing in one go, launching an (empty) interactive function definition prompt in the first command, instantly saving it when editing finishes. If there is already a function of that name it will show up in the editor, so it's easy to continuously tweak your functions without having to dive into (or even consider the existence of) your fish config dir. One of the very first functions I made when picking up the shell was "func", that makes exactly those two calls (since I only rarely edit a function without the intention of saving my changes)

For simple ssh stuff and other oneliners that don't take any input or where the input ($argv) is sure to just be appended at the end of the line,

alias zeus='ssh ...'; and funcsave zeus

will do the same. Any further editing will have to go through funced though.

Upvotes: 1

Froziph
Froziph

Reputation: 473

I think this should work.

Add this to the first line of the script:
#!/bin/fish
That way it knows to execute your script with fish. Otherwise you would have to use ./scriptname or fish scriptname, to indicate you wanted fish to execute it.

Remove the .fish ending, if you want to call it just by "zeus".

Add the file to your path. In your .fishrc, write:

export PATH=XXX:$PATH

Okay, so I've never used fish. Apperently they don't use .fishrc, put it into your equivalent of .bashrc. Which is ~/.config/fish/config.fish as far as I can see. Actually the line you have to write is

set -x PATH $PATH XXX

With XXX being the path to your file, for example copy it into a folder named bin in your home directory. Then it would be set -x PATH $PATH $HOME/bin

Last but not least, make the file executeable by writing:
chmod +x FILENAME


It seems like an alias would be a simpler solution for you however. In your fish equivalent of .bashrc, you can do:

function zeus
ssh username@server
end

This seems to be the fish equivalent of an alias. If it is just 1 line that is long, you could do it like this. Having the line inside the function. Now you could just call "zeus" from anywhere aswell.

Upvotes: 4

ridiculous_fish
ridiculous_fish

Reputation: 18551

You can make an executable and put it somewhere in $PATH. However, the simplest thing is to make a zeus function. A fish function is like a function in other languages: some named block of code.

  1. Run this: function zeus; your_long_command_goes_here ; end
  2. Run funcsave zeus to save it permanently

Now zeus will run that command.

What this does is put a file zeus.fish in ~/.config/fish/functions/. You can also do that manually of course.

Upvotes: 6

Related Questions