Home > Enterprise >  $ eval "$(ssh-agent -s)" vs $ eval ssh-agent -s
$ eval "$(ssh-agent -s)" vs $ eval ssh-agent -s

Time:01-13

I really confused to these 2 syntax. What makes they different?

Under windows VS code and git bash environment,

"$(ssh-agent -s)" works on me

$ eval "$(ssh-agent -s)"
Agent pid 966

but eval ssh-agent -s dosen't

$ eval ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-yXsZUCscU2gV/agent.960; export SSH_AUTH_SOCK;
SSH_AGENT_PID=961; export SSH_AGENT_PID;
echo Agent pid 961;

Any explanation will be helpful! Thanks

CodePudding user response:

Because ssh-agent -s prints shell commands to be executed. eval evaluates those in the context of the current shell, as if entered directly.

$(...) is called command substitution and will execute the command inside the parentheses, which are subsequently replaced with the output of the command.

So the order of steps is:

1. eval "$(ssh-agent -s)"
   1.1. ssh-agent -s
        (output): SSH_AUTH_SOCK=/tmp/ssh-yXsZUCscU2gV/agent.960; export SSH_AUTH_SOCK; SSH_AGENT_PID=961; export SSH_AGENT_PID; echo Agent pid 961;
2. eval "SSH_AUTH_SOCK=/tmp/ssh-yXsZUCscU2gV/agent.960; export SSH_AUTH_SOCK; SSH_AGENT_PID=961; export SSH_AGENT_PID; echo Agent pid 961;"
   (output): Agent pid 961

If you didn't substitute ssh-agent -s with its output, then eval will simply evaluate/call the ssh-agent binary and show its output – without evaluating the output.

  • Related