Fish-shell: eval $argv unparses the arguments

Created on 7 Jan 2017  ·  3Comments  ·  Source: fish-shell/fish-shell

  • [x] Have you checked if problem occurs with fish 2.4.0?
  • [x] 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?

question

Most helpful comment

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

All 3 comments

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings