简体   繁体   中英

$ eval "$(ssh-agent -s)" vs $ eval ssh-agent -s

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM