6

I was running eval `ssh-agent -s\` I get this error

fish: Unknown command: `ssh-agent
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jun 21, 2022 at 10:41

3 Answers 3

10

There are two parts to your question:

  • Why do you get an error when attempting eval `ssh-agent -s` ?
  • How to enable ssh-agent in the Fish shell

For the first, the feature you are looking for is called "command substitution". There are two ways of doing this in Bash/POSIX shells:

  • Backticks (e.g. `command`), as you are using
  • $(command)

The backticks form is highly discouraged, so if you are coming across documentation or a blog utilizing them, realize that it's severely out of date (or unaware).

In Fish, as you've discovered, the (command) grouping is the "most supported" way to do command substitution. However, as of Fish 3.4.0, the $(command) form also works. So using either:

  • eval (ssh-agent -c) or
  • eval $(ssh-agent -c) will work
How to run ssh-agent in fish shell?

However, I would recommend the use of keychain for simplifying ssh-agent use in Fish (and other shells). Ignore the part of the webpage that says (as @Charliesneath points out in the comments), "Currently, keychain is not compatible with Fish shell." The webpage just hasn't been updated in a while.

Keychain actually does have built-in support for Fish (added in 2017) and can make use of universal variables to keep your keys in sync across multiple shell sessions.

For instance, if you have two Fish shell sessions open, and you run ssh-agent/ssh-add in one of them, you still need to run the same in the other, and enter your password again. There are ways to share the agent among shell sessions, but Keychain handles it for you.

Keychain can be installed directly from most distributions' repositories. For instance, sudo apt install keychain.

It can be enabled in Fish with:

keychain --eval <keyfile> | source

I have this set up in Fish as follows.

  • Create the following script in ~/.config/fish/conf.d/keychain.fish

    if status is-login
        and status is-interactive
        # To add a key, set -Ua SSH_KEYS_TO_AUTOLOAD keypath
        # To remove a key, set -U --erase 
    SSH_KEYS_TO_AUTOLOAD[index_of_key]
        keychain --eval $SSH_KEYS_TO_AUTOLOAD | source
    end
    
  • set -Ua SSH_KEYS_TO_AUTOLOAD ~/.ssh/id... for whatever key(s) you want to use.

That's pretty much it. When you start a login Fish shell, if the key isn't unlocked, Keychain will ask for the password and add it to a shared ssh-agent. If it is already unlocked, then it won't ask again.

You can, of course, simplify the script by embedding static key name(s). I prefer universal variables to keep the script dynamic. This allows a single script to be stored in my dotfiles repo even though I use different keys on different systems.

6
  • From the keychain page: "Currently, keychain is not compatible with Fish shell." funtoo.org/Funtoo:Keychain Commented Sep 15, 2022 at 0:02
  • 1
    @charliesneath Sure, but the website has been outdated for almost 5 years now. Fish support was added in this commit from 2017. From the manpage, "Keychain supports most UNIX-like operating systems, including Cygwin. It works with Bourne-compatible, csh-compatible and fish shells." It actually works better with Fish than with other shells, due to its use of Fish universal variables. Commented Sep 15, 2022 at 0:42
  • 1
    I don't think there's a point in checking both is-login and is-interactive, as far as I know login shells are interactive shells by definition (but not all interactive shells are login shells).
    – pzkpfw
    Commented Feb 18 at 13:02
  • @pzkpfw What about fish -l -c "status is-login && echo 'is-login'; status is-interactive || echo 'but not interactive'"? It seems to me that any shell invocation with both -l and -c meets that criteria, right? Commented Feb 19 at 23:11
  • @pzkpfw Also, I notice that your edit was rejected - You might want to post that as a new answer, with a reference back to this one and noting the differences. Thanks! Commented Feb 19 at 23:15
6

I get this solution

run

eval (ssh-agent -c)

reference:https://wiki.archlinux.org/title/Fish#Evaluate_ssh-agent

0

I use fish_ssh_agent from gihub https://github.com/ivakyb/fish_ssh_agent

github/ivakyb/fish_ssh_agent

That repo also has few valuable hint on topic SSH, SSH-agent, Fish-shell.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .