News for hackers. Stuff that matters.
Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by js2 on Tuesday June 20, 2023 @06:08PM (#36411377)
I think you want:

  trap 'ssh-agent -k' EXIT INT TERM
I don't see any reason for the eval as "ssh-agent -k" doesn't return anything useful you want the shell to evaluate.
Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by chasil on Tuesday June 20, 2023 @06:25PM (#36411557)
That's not what the eval is for.

The "ssh-agent -k" command will emit shell commands that the shell must then execute which will kill the agent daemon and unset the socket environment variable.

Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by leodag on Tuesday June 20, 2023 @06:35PM (#36411655)
> The "ssh-agent -k" command will emit shell commands

Does it really? I've executed it here and it just runs kill, doesn't emit any bash. Running just ssh-agent (without any args) does that though, which is what's probably causing the confusion.

Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by chasil on Tuesday June 20, 2023 @06:48PM (#36411799)
I am on OpenBSD 7.2, and I see:

  $ eval $(ssh-agent)
  Agent pid 56785

  $ ssh-agent -k
  unset SSH_AUTH_SOCK;
  unset SSH_AGENT_PID;
  echo Agent pid 56785 killed;
The correct processing of that output requires an eval.

Did you have any other questions?

Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by pmarreck on Tuesday June 20, 2023 @08:09PM (#36412560)
Why do you need to eval it?

$(ssh-agent)

won’t substitute that with the stdout and run that?

Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by chasil on Tuesday June 20, 2023 @08:35PM (#36412789)
Because the intended use for "ssh-agent -k" is for eval.

While redirecting to /dev/null will certainly work, the agent is holding sensitive credentials (by design), and confirmation of shutdown has a tangible security benefit.

Re:“Exit Traps” Can Make Your Bash Scripts Way More Robust a... by js2 on Tuesday June 20, 2023 @06:43PM (#36411756)
If all you care about is killing it, you don't need to eval the output. The output just unsets two environment variables which only matters in the current shell context.

  $ ssh-agent
  SSH_AUTH_SOCK=/var/folders/8p/_pwq997168s7vdwwdg_qr1j40000gn/T//ssh-DE0IoJfU5rrM/agent.15015; export SSH_AUTH_SOCK;
  SSH_AGENT_PID=15016; export SSH_AGENT_PID;
  echo Agent pid 15016;

  $ SSH_AGENT_PID=15016; export SSH_AGENT_PID;
  $ ssh-agent -k
  unset SSH_AUTH_SOCK;
  unset SSH_AGENT_PID;
  echo Agent pid 15016 killed;
That said, it doesn't hurt to eval it, so I overstated my case in my original comment.