17

I'd like to use keychain with the fish shell, but I'm not sure how to get fish to load the ~/.keychain/hostname-fish file to set the appropriate environment variables.

In bash, there's the "source" command, but it doesn't exist in fish.

Elijah Lynn
  • 1,444
  • 3
  • 17
  • 26
Lorin Hochstein
  • 4,287
  • 6
  • 29
  • 26

4 Answers4

15

Below is what I have in ~/.config/fish/config.fish for your specific example.

set -gx HOSTNAME (hostname)
if status --is-interactive;
    keychain --nogui --clear ~/.ssh/id_rsa
    [ -e $HOME/.keychain/$HOSTNAME-fish ]; and source $HOME/.keychain/$HOSTNAME-fish
end

The source command is source, which also works in bash.


Prior to fish 2.1.0, the source command was called ..

jamessan
  • 1,071
  • 7
  • 12
  • @JohnMetta, see the link in the question and http://www.funtoo.org/wiki/Keychain. You'll need to install the program. – jamessan Feb 28 '13 at 02:11
  • Sorry, it was stupid of me to post that. I didn't actually *want* keychain- I was looking to replace the 'source' command, and didn't actually read. Using `.` is what I needed. – JohnMetta Mar 01 '13 at 00:17
  • 1
    Thanks for including the `[ -e $HOME/.keychain/$HOSTNAME-fish ]; and . $HOME/.keychain/$HOSTNAME-fish` line. That got my keychain to work and git to stop asking me for the passphrase every time I wanted to push. – cjm Aug 26 '16 at 18:34
5

Use the source command:

source filename.txt

source may not have existed originally but it exists now and the . alias in fish is officially deprecated.

From man . in fish 3.6.1:

. (a single period) is an alias for the source command. The use of . is deprecated in favour of source, and . will be removed in a future version of fish.

Elijah Lynn
  • 1,444
  • 3
  • 17
  • 26
4

If the file you're trying to source contains bash, consider using Bass.

This will let you write, for example:

bass source ~/.bash_profile
newswim
  • 141
  • 2
2

The way recommended on the Keychain documentation page is to put this in config.fish:

if status --is-interactive
    keychain --eval --quiet --quick path/to/id_rsa
end

Then, add this to the top of your script:

source $HOME/.keychain/(hostname)-fish

source: http://www.funtoo.org/Keychain

Elijah Lynn
  • 1,444
  • 3
  • 17
  • 26