Skip to content

How to eval the multiline output of a command #3993

Closed

Description

@miquella

I've been working on Vaulted trying to get vaulted env to work properly for fish (apparently I screwed up the initial implementation, now I'm trying to fix it 😛).

The output is fish syntax for setting variables, something like:

# To load these variables into your shell, execute:
#   eval (vaulted env test)
set -gx TEST "test value";
set -gx OTHER "another test";

Unfortunately, this ends up passing all of the lines into eval as individual arguments. eval concatenates them all with spaces, making it one big run-on command. Since the output starts with a comment character, this effectively comments out the entire script and nothing gets run.

After reviewing #159 and fiddling with various things, I've found one thing that actually works without polluting the environment with extraneous variables:

begin; vaulted env test | read -lz cmds; and eval $cmds; end

But this feels like I'm taking the long way to getting the script into eval. It seems like fish would have a more straightforward way of piping the output of a command into eval, something that would keep the script intact rather than splitting it into lines and then recombining it in a different way. I hope I just missed something in fish and am just making this harder than it needs to be! lol

fish, version 2.5.0


FWIW, bash handles it through a quoted subshell this way:

eval "$(vaulted env test)"

Activity

  1. zanchey commented on Apr 27, 2017

    @zanchey
    Member

    Is there a reason you need to use eval rather than source, which you can pipe into?

  2. krader1961 commented on Apr 27, 2017

    @krader1961
    Contributor

    You should not include comments in output meant to be handed to eval; even in the context of other shells like bash. As @zanchey suggested if you want to retain that pattern it is better to tell your users to do vaulted env test | source.

  3. miquella commented on Apr 27, 2017

    @miquella
    Author

    Excellent, this is exactly the type of thing I was hoping I was overlooking! It didn't even occur to me to pipe the script into source (which, unfortunately, doesn't work in bash or zsh).

    I do agree on not including comments in the script that's passed to the shell. In fact, our original implementation only included the comments when a TTY was present. Since vaulted's primary purpose is keeping unencrypted secrets off your disk, we wanted to provide suggested commands to help people ensure that happened.

    Thank you so much!

  4. miquella commented on Apr 27, 2017

    @miquella
    Author

    Piping into source solves the issue in this case, but I'm still left wondering if there's a way to pass the unaltered contents of a command in as an argument to another command (without temporarily storing it in a variable, that is)?

  5. krader1961 commented on Apr 27, 2017

    @krader1961
    Contributor

    I'm still left wondering if there's a way...

    Not at this time. See issue #159.

  6. miquella commented on Apr 27, 2017

    @miquella
    Author

    Hmm, okay. Thanks, @krader1961!

  7. added a commit that references this issue on May 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions