Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eval $argv unparses the arguments #3708

Closed
2 tasks done
rwz opened this issue Jan 6, 2017 · 3 comments
Closed
2 tasks done

eval $argv unparses the arguments #3708

rwz opened this issue Jan 6, 2017 · 3 comments
Labels

Comments

@rwz
Copy link

rwz commented Jan 6, 2017

  • Have you checked if problem occurs with fish 2.4.0?
  • Tried fish without third-party customizations (check sh -c 'env HOME=$(mktemp -d) fish')?

fish version installed (fish --version): 2.4.0

OS/terminal used: OSX 10.12.2 (16C67) / iTerm2 Build 3.0.13

Let's say I have a simple script that just outputs its arguments. It could look something like this:

#!/usr/bin/env ruby
p ARGV

and its execution looks like this:

> ./myscript.rb foo 'hello world'
["foo", "hello world"]

Notice how "hello world" is grouped as one argument vs "hello" and "world" being separate arguments.

Now, I want to write a fish function that lets me execute any arbitrary fish command (including aliases) with some ENV vars set up.

This is my first attempt of such function

function debug
  set -xg DEBUG 1
  eval $argv
  set -e DEBUG
end

Here's what happens when I run my script from above through this function:

> debug ./myscript.rb foo 'hello world'
["foo", "hello", "world"]

Notice that single "hello world" argument turned into "hello" and "world", which is not what I wanted.

I've also tried the following approach:

env DEBUG=1 fish -c "$argv"

Which produced the same result.

Is there any way to make the function's $argv preserve "hello world" grouping? What am I missing?

@faho
Copy link
Member

faho commented Jan 6, 2017

This is a problem with eval, not the "$argv" in particular.

eval parses its arguments as if they were given on the commandline, and it doesn't care about how the arguments are split.

We've had an issue about this before, but the consensus was that we can't change eval now, and that it might make other things trickier.

What we are trying to do know is in #154 - make command and such accept variables.

That would mean you could add command $argv, and it would run it like you expect.

@faho faho changed the title Function $argv unparses the arguments eval $argv unparses the arguments Jan 6, 2017
@faho
Copy link
Member

faho commented Jan 6, 2017

Note that currently you can use string escape, i.e. eval (string escape -- $argv).

@faho faho added the question label Jan 6, 2017
@rwz
Copy link
Author

rwz commented Jan 6, 2017

Thanks. (string escape -- $argv) worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants