Search code examples
fish

Proxying arguments from one function to a command


I have function bajouras.fish which calls another function, let's say scarabosses:

function bajouras
  scarabosses "someCMD" $argv
end

so the function scarabosses will get the first argument "someCMD" and any extra arguments passed to bajouras.

Inside scarabosses I'm doing something like this:

function scarabosses
  if test "$count" -ge 2
    set args $argv[2..-1]
  else
    set args ""
  end

  eval (type -fP $argv[1]) $args
end

Now the problem is that if I pass some string to the bajouras

bajouras -v -t "some cool (string)"

and echo the $argv my string although is a parameter, no longer is a string. So by the time I pass that to scarabosses this is what I get:

someCMD -v -t some cool (string)

And since I'm no longer passing a string to scarabosses fish will try to do some command substitution and I will get an error.

Is there a way to ensure that whatever I pass to bajouras will stay that way?


Solution

  • The eval is the cause of your problem. That just takes all of its arguments and interprets them as if they were given on the commandline, i.e. it tokenizes it again, performs expansions, etc.

    I recommend avoiding eval if you can, which you cannot in this case (currently).

    What you should do here is to use string escape to safeguard any arguments you don't want eval to interpret again:

       eval (string escape -- (type -fP $argv[1]) $args)
    

    (I'm also escaping the type -fP here since it could contain spaces that eval would split on)