Skip to content

Cannot use variable when typing path to command #154

Closed

Description

@lilyball

Fish does not accept $HOME/bin/mvim as a valid typed command. This is perfectly legitimate in bash and other shells, and I wonder why Fish disallows it.

> $HOME/bin/mvim
fish: Variables may not be used as commands. Instead, define a function. See the help section
for the function command by typing 'help function'.

Even more curious, if I quote the variable, I get a slightly corrupted error message back.

> "$HOME"/bin/mvim
fish: Unknown command 'HOME/bin/mvim'

That garbage character is U+F003

Activity

  1. jonclayden commented on Jun 26, 2012

    @jonclayden

    I've bumped into this several times in the process of converting bash aliases to fish functions. It's quite annoying. For scripts you can obviously work around it with /bin/sh $HOME/bin/myscript or similar, but it's a real problem when you're referring to a native executable. If this is by design I'd like to know how to get around it.

  2. maxfl commented on Jun 26, 2012

    @maxfl
    Contributor

    As far as I understand this is done as protection in order to make sure variables will not be used as if,else,case,switch,end,etc since they are parsed in the same way as other commands. And this is partly good point.

    From my point of view, the bad thing is that you can not execute neither 'command $cmd' nor 'builtin $cmd'. Moreover
    there is now way to execute 'call $cmd' which could execute builtin or command as fish does for usual executions.

    Current workaround is to call 'eval $HOME/bin/mvim', which is not very comfortable if you have arguments with escaped characters, eval will loose them.

    Another workaround is to use a function (attached). It's not a direct way, but it works and correctly handles escaped variables. But the redirection/suspending is specified indirectly, as first argument if needed:
    call $vim
    call -r ' ^/dev/null' $make # to redirect
    call -r '>/dev/null &' make # to suspend
    call echo "asdf sdf"
    call $vim 'long filename with spaces'
    And it works in a nested way:
    call eval eval call call call echo 123

     function call --description 'Run a builtin or program' --no-scope-shadowing
              set -l redir
              switch "$argv[1]";
                  case -r --redirection
                  set redir $argv[2]
                  set -e argv[1 2]
              end
              set -l cmd $argv[1]
              set -e argv[1]
    
              # 
              # Set job control for interactive mode
              #
              set -l __call_recover_mode
              if status --is-interactive-job-control
                  set __call_recover_mode interactive
              else
                  if status --is-full-job-control
                      set __call_recover_mode full
                  else
                      set __call_recover_mode none
                  end
              end
              if status --is-interactive
                  status --job-control full
              end
    
              #
              # Define main function and call
              #
              echo "function -S __run_internal_function_name; $cmd \$argv $redir; end"  | .
              set -e cmd
              set -e redir
              __run_internal_function_name $argv
    
              #
              # Clear and return
              #
              set -l stat $status
              functions -e __run_internal_function_name
              status --job-control $__call_recover_mode
              return $stat
          end
  3. lilyball commented on Jun 26, 2012

    @lilyball
    ContributorAuthor

    What is --no-scope-shadowing? That's not documented.

    -Kevin

    On Jun 26, 2012, at 6:02 AM, maxfl [email protected] wrote:

    function call --description 'Run a builtin or program' --no-scope-shadowing

  4. jonclayden commented on Jun 26, 2012

    @jonclayden

    Thanks for the code, and for highlighting eval. I guess it is the intended approach, and in fact the error points you at it if the variable appears within the path, rather than at the start:

    $ set test (whoami)
    $ /Users/$test/bin/myscript
    fish: Commands may not contain variables.
    Use the eval builtin instead, like 'eval /Users/$test/bin/myscript'.
    See the help section for the eval command by typing 'help eval'.
    

    This is actually discussed in this fish manifesto article:

    Since the use of variables as commands in regular shells is allowed, it is impossible to reliably check the syntax of a script. [...] fish solves this by disallowing variables as commands. Anything you can do with variables as commands can be done in a much cleaner way using either the eval command or by using functions.

    I just hadn't realised that this restriction would be so tight, since in my mind using a variable to form part of the path to an executable is different to using the variable as a command in its own right.

  5. maxfl commented on Jun 27, 2012

    @maxfl
    Contributor

    --no-scope-shadowing is not documented, though it should be.

    This option allows to operate on local variables of outer scope. For example it can be needed in following function:
    function inc --description 'Increase the value of variable' --no-scope-shadowing
    set $argv[1](expr $$argv[1] + 1)
    end

    So you can call 'inc index' to increase the value of $index by 1, even if $index is local variable.

  6. Ericson2314 commented on Jul 31, 2015

    @Ericson2314

    This restriction makes me a bit uncomfortable -- reminds me of a "two lisp" like common lisp, where functions and data are bound in different namespaces.

    Since the use of variables as commands in regular shells is allowed, it is impossible to reliably check the syntax of a script. [...] fish solves this by disallowing variables as commands. Anything you can do with variables as commands can be done in a much cleaner way using either the eval command or by using functions.

    Huh? Higher order functions != implict eval. Parsing should happen before evaluation absent eval:

    > (echo echo) asdf
    asdf
    > set foo echo
    > $foo asdf
    echo echo
    > set bar = if
    > $bar true; echo asdf; else; echo qwer; end
    # syntax error

    It is fine if else and friends look like builtin commands, but they certainly are something more. else without if gives a syntax error, not an unbound error, for example. That "secondary parsing" that ensures such "special builtins" appear in a proper syntax also happens before variable expansion.

  7. krader1961 commented on Apr 7, 2016

    @krader1961
    Contributor

    I cross-linked a few more issues that are duplicates of this one. And I know I missed many more. This really needs to become a priority to fix. I've managed to live without this capability since switching to fish a few months ago but was extremely annoyed when I noticed I couldn't safely invoke external commands via variable substitution. Worse, there is no good reason not to allow it.

    The documentation regarding why this restriction exists is almost incoherent. The restriction is because fish uses a parsing model that doesn't re-tokenize the statement after variable substitution. And that is a good thing. That POSIX shells, based on the original Bourne shell, do that re-tokenization is the source of many of the challenges to safely using them.

    The idea of a call command that evals just the first argument hints at the proper solution. That is, modify the command builtin to perform variable substitution on its first argument. Then people can do things like command $EDITOR $argv and command $HOME/bin/mvim.

    Symmetry argues for modifying the builtin builtin to do the same variable substitution. However, that is far less likely to be useful and could even be dangerous so we shouldn't do that unless someone can make a good argument for it.

  8. Ericson2314 commented on Apr 7, 2016

    @Ericson2314

    The restriction is because fish uses a parsing model that doesn't re-tokenize the statement after variable substitution.

    This is the non-problem I was trying to get at. Do not reparse but just treat the first element of the expanded array as the exe and the rest as (prepended) args.

  9. Ericson2314 commented on Apr 7, 2016

    @Ericson2314

    The change to command is still good, what I proposed above can be made distinct by making it work for functions too (but still not builtins). This is because all functions and external commands both take an arbitrary list; there is no special parsing that could be done.

  10. krader1961 commented on Apr 8, 2016

    @krader1961
    Contributor

    See the discussion in issue #564; especially the point made by ridiculousfish on 2013-02-07.

  11. krader1961 commented on Apr 8, 2016

    @krader1961
    Contributor

    @Ericson2314: Despite what I said a few comments earlier we definitely do not want to allow the indirection for builtins. Not even those like command, which could theoretically be implemented as a builtin but are currently special-cased by the parser.

    I'm ambivalent about allowing indirection through a variable to functions. I've read all the issues where this problem has been discussed and have not seen a truly compelling argument for supporting it. You could argue functions should be supported in as much as you might want to choose from several alternative wrapper functions at run time, just as you want to be able to choose among external editors by setting $EDITOR. Yet you can get the same effect by defining another function whose name is invariant and having it redirect to the desired implementation. Like we do today with functions such as fish_title. A stronger argument for indirecting thru a var to a function is to allow callbacks without having to use eval in the function receiving the callback. That's a capability I use quite a bit in Python and C but I'm not sure how I feel about supporting it in fish.

  12. Ericson2314 commented on Apr 8, 2016

    @Ericson2314

    Yeah to be clear I am 100% against implicit eval / reparsing too. Gross hack, poor security, etc, as you say.

  13. Ericson2314 commented on Apr 8, 2016

    @Ericson2314

    @krader1961 yeah to only be able to do callbacks (higher order functions) with eval means puts users in a security-vs-expressiveness bind. The fact that $asdf extra-args... has such predictable semantics regardless of the value of $asdf is a huge benefit.

  14. Ericson2314 commented on Apr 8, 2016

    @Ericson2314

    @jakwings its the fact that bash will support more than functions and external commands in there that is so suspect. It's a weakened, but still dangerous eval, (unlike what we propose for fish) with bash.

  15. 27 remaining items

  16. floam commented on Jan 6, 2017

    @floam
    Member

    I mean, by that token - having a builtin or command statement at all is inconsistent and they shouldn't exist since we can't call a function directly. To me, it just seems strange that we wouldn't expand here. If we added a functions --run or whatever other names have been proposed - it'd ought to do the same thing. What makes exec special over command with regards to expansion? I might be missing something obvious?

  17. faho commented on Jan 6, 2017

    @faho
    Member

    But I would be happier if someone could provide a good example of why command $foo (and builtin $foo) are useful before we implement that feature.

    Same reason command foo is useful - sometimes you don't want a function, or you want to call a command that has been shadowed by a builtin.

    Also, again, for consistency's sake, so this stuff is easily understandable. If all of these "decorations" are allowed, then you just need to remember "I can launch something with a variable as long as it's started indirectly, by another 'command'". Whether that "command" is command, call, env or whatever. If only some are, then you need to remember which exactly.

    introduces an inconsistency since you can't indirectly run a function that way. That is, there is no function $foo equivalent to command $foo

    The obvious solution here would be to add something to run "a function". We don't really need it because if a function for something exists it'll be used by default and if it doesn't a functions --run or funcall or call --function would fail anyway, but for consistency it might be nice.

  18. faho commented on Jan 6, 2017

    @faho
    Member

    I have no objection to allowing call (string split " " -- $EDITOR) (i.e., command substitutions), @faho, but do feel strongly that fish should not expand and whitespace split call $EDITOR.

    Obviously, I agree - which is why I said "I'm not asking for it to be split on spaces again".

    The situation with $EDITOR is a bit annoying since for ease of use in fish it should be a list of "command option1 option2", but to be useful outside of fish it can only be a single string.

    The only obvious and complete solution is to fix unix to allow list-variables. And that'd be quite a bit of work.

  19. krader1961 commented on Jan 7, 2017

    @krader1961
    Contributor

    The case of an env var (e.g., $EDITOR) as a string containing a command plus options separated by whitespace (or colons, etc.) is another pain point discussed in issue #436. Hopefully someday soon we'll implement a mechanism to make importing and exporting such vars easier such that when used inside fish they are proper arrays.

  20. Ericson2314 commented on Jan 10, 2017

    @Ericson2314

    Just want to say I'm excited that this is being worked out! The arguments on why eval is bad, array expansion is good, fish functions are overkill for this, etc, are exactly what I'd hope to bring up. Still feel like command/call aren't needed, but that's a small point I wouldn't want this to be hung up on!

  21. pgan002 commented on May 28, 2017

    @pgan002

    If variables can be executed with a decorator command, then that duplicates much of what functions do. Should we remove functions as a concept in Fish?

    As for a decorator name, how about do $var, do function $var, do command $var, do builtin $var to replace builtin and command? Or even do exec $var instead of exec? This is slightly more verbose but groups these commands that have quite similar meaning into one command with parameters.

  22. faho commented on May 28, 2017

    @faho
    Member

    If variables can be executed with a decorator command, then that duplicates much of what functions do. Should we remove functions as a concept in Fish?

    I can't see how it does. I mean there's function PAGER; less; end vs call $PAGER, but that's such a minor use-case that it barely matters.

    I notice that we haven't specified what happens if a variable contains syntax relating to multiple commands. That doesn't even work in bash (foo="echo a | less"; $foo prints "a | less") and I don't think it should in fish either. With that out of the way, variables cannot contain branches, so they are not a replacement for functions in any way but the most mundane (the above).

    As for a decorator name, how about do $var, do function $var, do command $var, do builtin $var to replace builtin and command?

    I find the idea of using subcommands here interesting. I'm not sure that it would be okay to allow no subcommand (to run anything). What happens if the first argument then looks like a subcommand? E.g. do $var - with var set to foo a b c, it would execute a foo function/builtin/command, but with var set to function a b c, it would try to execute a function a. That seems like a recipe for confusion - if you get a variable from somewhere else, you have to remember to use do any $var or check against the subcommands yourself. If we instead disallow do $var (where the first word of $var does not look like a subcommand), then it would be clear. I.e. make it fail in 90%, not 10%, of cases so the failure is visible.

    I still think call is clearer than do, which is as generic as it gets, though.

  23. added a commit that references this issue on Jul 20, 2017
  24. cben commented on Aug 13, 2018

    @cben
    Contributor

    Implicit cd also doesn't work with vars:

    ~ $ $GOPATH/src/github.com/exercism/cli/
    fish: Variables may not be used as commands. In fish, please define a function or use 'eval $GOPATH/src/github.com/exercism/cli/'.
    ~ $ set PKG github.com/exercism/cli/
    ~ $ ~/go/src/$PKG
    fish: Variables may not be used as commands. In fish, please define a function or use 'eval /home/bpaskinc/go/src/$PKG'.
    ~ $ command ~/go/src/$PKG
    fish: Variables may not be used as commands. In fish, please define a function or use 'eval /home/bpaskinc/go/src/$PKG'.
    command ~/go/src/$PKG
            ^
    ~ $ eval ~/go/src/$PKG
    ~/go/src/git/exe/cli (master|✔) $ 

    Now of course cd ~/go/src/$PKG is the simplest solution here, zero reason to resort to eval / command or proposed call / do.
    In a script, I'd probably write explicit cd dir/ instead of implicit ./dir/ for clarity anyway.
    It's just a one more (minor) annoyance where this restriction violates law of orthogonality.

  25. ridiculousfish commented on Sep 1, 2018

    @ridiculousfish
    Member

    Revisiting this, I no longer think do or call is necessary. It's fine to allow variables in commands. If a command expands to zero strings, it's an error; if it expands to multiple strings, the first one is the command and the remainder become arguments. No further expansion takes place for these arguments.

  26. mqudsi commented on Oct 18, 2018

    @mqudsi
    Contributor

    @ridiculousfish what about this (overly contrived) example:

    (which foo) --bar

    I hit a real-world case today where I would have been forced to use eval before, but thanks to 865a464 I was able to store the result of the command substitution in a variable then execute that instead, like so:

    set foo (which foo)
    $foo --bar

    But given that it's equivalent to the (currently illegal) first example, is there a good reason to not enable that as well?

  27. ridiculousfish commented on Oct 18, 2018

    @ridiculousfish
    Member

    Don't do that, that's what env is for: env foo --bar...

  28. mqudsi commented on Oct 18, 2018

    @mqudsi
    Contributor

    Of course. Thanks!

  29. locked as resolved and limited conversation to collaborators on Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions