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

2022-06-12 Thread Greg Wooledge
On Sun, Jun 12, 2022 at 07:57:40AM -0400, rhkra...@gmail.com wrote:
> On Saturday, June 11, 2022 10:25:34 AM Greg Wooledge wrote:
> > On Sat, Jun 11, 2022 at 09:54:17AM -0400, rhkra...@gmail.com wrote:
> > > eval `ssh-agent`
> 
> > For the record, the command you've got here is written in a very
> > antiquated way.  A better (as well as more modern) way to write it
> > would be:
> > 
> > eval "$(ssh-agent)"
> 
> I am curious about the origin of the more modern syntax -- was it driven (or 
> partially driven) by the possiblity of (a human) confusing the backticks for 
> single quotes (like I did)?

Formal definitions:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03

Rationale:

https://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_06_03

See also:

https://mywiki.wooledge.org/BashFAQ/082



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

2022-06-12 Thread The Wanderer
On 2022-06-12 at 07:57, rhkra...@gmail.com wrote:

> On Saturday, June 11, 2022 10:25:34 AM Greg Wooledge wrote:
> 
>> On Sat, Jun 11, 2022 at 09:54:17AM -0400, rhkra...@gmail.com
>> wrote:
>>> eval `ssh-agent`
> 
>> For the record, the command you've got here is written in a very 
>> antiquated way.  A better (as well as more modern) way to write it 
>> would be:
>> 
>> eval "$(ssh-agent)"
> 
> I am curious about the origin of the more modern syntax -- was it
> driven (or partially driven) by the possiblity of (a human) confusing
> the backticks for single quotes (like I did)?

I don't know the history myself (a brother of mine might, but I don't
know if he's up at this hour), but my guess would be more that it's
about other things - such as, for example, nesting.


The syntax (modulo quoting)

$ foo $(bar $(baz))

is unambiguous, but the syntax

$ foo `bar `baz``

is sufficiently ambiguous that it's not going to practically be possible
for the shell to determine what the writer intended to do.


There's a possibility that something like

$ foo `bar \`baz\``

might be supported and might work, but even if so, that would just mean
having to keep track of how many escaping backslashes you've used on
each backtick - and it would probably make managing quoting the
subshells even harder to get right. It'd be unwieldy at best, compared
to the explicit start-and-end markers that are involved with the $()
syntax.

-- 
   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



signature.asc
Description: OpenPGP digital signature


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

2022-06-12 Thread rhkramer
On Saturday, June 11, 2022 10:25:34 AM Greg Wooledge wrote:
> On Sat, Jun 11, 2022 at 09:54:17AM -0400, rhkra...@gmail.com wrote:
> > eval `ssh-agent`

> For the record, the command you've got here is written in a very
> antiquated way.  A better (as well as more modern) way to write it
> would be:
> 
> eval "$(ssh-agent)"

I am curious about the origin of the more modern syntax -- was it driven (or 
partially driven) by the possiblity of (a human) confusing the backticks for 
single quotes (like I did)?


> The lack of outer double quotes in the antiquated command would normally
> cause it to fail, but ssh-agent writes some redundant semicolons, so
> that the unquoted legacy command still works.



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

2022-06-11 Thread Greg Wooledge
On Sat, Jun 11, 2022 at 09:54:17AM -0400, rhkra...@gmail.com wrote:
> eval `ssh-agent`

> Then, in accordance with my vague recollection of what I think of as Greg 
> Woolidge's typical advice (which I (may mis-)remember as "always quote"), I 
> thought the backticks (that I incorrectly saw as single quotes) were just the 
> "proper" way to specify a command to be eval'ed.  

For the record, the command you've got here is written in a very
antiquated way.  A better (as well as more modern) way to write it
would be:

eval "$(ssh-agent)"

The lack of outer double quotes in the antiquated command would normally
cause it to fail, but ssh-agent writes some redundant semicolons, so
that the unquoted legacy command still works.



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

2022-06-11 Thread rhkramer
Intentionaly top posting: I just wanted to say that the question is resolved, 
and record my understanding.

Thanks to all who replied!

Repeating the command for posterity: 

eval `ssh-agent`

I now see that it is a two step process, first the backticks cause ssh-agent to 
be run, producing 3 lines of output.

Then, in accordance with the explanation of eval I read in man bash, those 
three lines of output are concatenated and evaluated (executed).

At first I hadn't noticed that those were backticks, I assumed, without looking 
closely, that they were single quotes.

Then, in accordance with my vague recollection of what I think of as Greg 
Woolidge's typical advice (which I (may mis-)remember as "always quote"), I 
thought the backticks (that I incorrectly saw as single quotes) were just the 
"proper" way to specify a command to be eval'ed.  

For posterity, here is what man bash says:


eval [arg ...]
   The  args  are read and concatenated together into a single command.  
This command is then read and executed by the shell, and its exit status  is  
returned  as  the value of eval.  If there are no args, or only null 
arguments, eval returns 0.
<\quote>

Another part of my misunderstanding stemmed from seeing only one command on 
that line, and wondering what had to be concatenated.  (Now I get it.)

Nothing new below this line -- in fact, almost all deleted:

On Friday, June 10, 2022 11:59:42 AM The Wanderer wrote:
> On 2022-06-10 at 11:25, rhkra...@gmail.com wrote:



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

2022-06-10 Thread David
On Sat, 11 Jun 2022 at 01:57, Kamil Jońca  wrote:
> rhkra...@gmail.com writes:

> > 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.
> >
> > I wondered why, as I thought I would get the same result from just running
> > =ssh-agent=, but the results are different -- see below:

$ help eval
eval: eval [arg ...]
Execute arguments as a shell command.

Combine ARGs into a single string, use the result as input to the shell,
and execute the resulting commands.

Exit Status:
Returns exit status of command or success if command is null.
$



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

2022-06-10 Thread The Wanderer
On 2022-06-10 at 11:25, rhkra...@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



signature.asc
Description: OpenPGP digital signature


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

2022-06-10 Thread Kamil Jońca
rhkra...@gmail.com writes:

> 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.
>
> 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`
> 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?

ssh-agents prints details about its config (i.e. where ssh should search
authentication agent) - and thats all. (these are SSH_AUTH_SOCK et al.)

eval `` in turn takes these statements and try to evaluate them
(i.e. creates these two variables in environment)

as an example try
--8<---cut here---start->8---
$echo "KJONCA_VAR=this_is_test"
$set | grep KJONCA
$eval $(echo "KJONCA_VAR=this_is_test")
$set |grep KJONCA
--8<---cut here---end--->8---

KJ

-- 
http://stopstopnop.pl/stop_stopnop.pl_o_nas.html



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

2022-06-10 Thread Loïc Grenié
On Fri June 10th 2022 at 17:26,  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.
>
> 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`
> 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?
>

The command "ssh-agent" returns 3 lines. The "eval `ssh-agent`" command
  makes bash (or any other sh-compatible shell) runs those three lines as
if they
  had been typed. Hence it defines two variables (SSH_AUTH_SOCK and
  SSH_AGENT_PID) and prints "Agent pid 23933".

 Greetings,

  Loïc


OT: Bash: what is eval doing here?

2022-06-10 Thread rhkramer
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.

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`
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?