[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Bash: what is eval doing here?



On 2022-06-10 at 11:25, rhkramer@gmail.com wrote:

> In my (seemingly unending) quest to understand ssh, I've come across a 
> document that calls for running =eval 'ssh-agent'= from a command line.

Note that here you have 'ssh-agent', with single-quotes AKA apostrophes...

> I wondered why, as I thought I would get the same result from just running
> =ssh-agent=, but the results are different -- see below:
> 
> $ eval `ssh-agent`

...but here you have `ssh-agent`, with backticks.

Backticks are an older syntax for "run this command in a subshell, and
put the output of that command in this place on the command line, before
running the rest of this command". The newer syntax for the same thing
is $().

> Agent pid 23929
> 
> $ ssh-agent
> SSH_AUTH_SOCK=/tmp/ssh-uLqQ9VWX0RL7/agent.23932; export SSH_AUTH_SOCK;
> SSH_AGENT_PID=23933; export SSH_AGENT_PID;
> echo Agent pid 23933;
> 
> Can anybody on here explain what is going on / why?

Because of the subshell effect, what this effectively does is run the
commands that got printed by running ssh-agent, as Loïc Grenié already
indicated.

(There's also considerations involving the semicolons and the fact that
eval explicitly combines its arguments into one string before treating
them as commands to be run by the shell, but I don't have my head fully
around those yet, so I'm not going to try to explain them. It seems to
produce intuitive and useful behavior, in any case.)

-- 
   The Wanderer

The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man.         -- George Bernard Shaw

Attachment: signature.asc
Description: OpenPGP digital signature


Reply to: