You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot use variable when typing path to command #154
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.
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.
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
functioncall --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
elseif status --is-full-job-control
set __call_recover_mode full
elseset __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_modereturn$stat
end
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'.
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.
--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.
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:
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.
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.
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.
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.
@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.
@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.
@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.
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?
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.
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.
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.
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!
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.
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.
~ $ $GOPATH/src/github.com/exercism/cli/
fish: Variables may not be used as commands. In fish, please define a functionor 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 functionor use 'eval /home/bpaskinc/go/src/$PKG'.
~ $ command~/go/src/$PKG
fish: Variables may not be used as commands. In fish, please define a functionor 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.
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.
Reacted by Soumya, Thom Chiovoloni, Sean Zhu, John Allen, Francis St-Amour, Beni Cherniavsky-Paskin and BeatButton
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?
Activity
jonclayden commentedon Jun 26, 2012
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/myscriptor 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.maxfl commentedon Jun 26, 2012
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
lilyball commentedon Jun 26, 2012
What is --no-scope-shadowing? That's not documented.
-Kevin
On Jun 26, 2012, at 6:02 AM, maxfl [email protected] wrote:
jonclayden commentedon Jun 26, 2012
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:This is actually discussed in this fish manifesto article:
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.
maxfl commentedon Jun 27, 2012
--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:$argv[1](expr $ $argv[1] + 1)
function inc --description 'Increase the value of variable' --no-scope-shadowing
set
end
So you can call 'inc index' to increase the value of $index by 1, even if $index is local variable.
Ericson2314 commentedon Jul 31, 2015
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.
Huh? Higher order functions != implict eval. Parsing should happen before evaluation absent eval:
It is fine if
elseand friends look like builtin commands, but they certainly are something more.elsewithoutifgives 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.krader1961 commentedon Apr 7, 2016
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
callcommand that evals just the first argument hints at the proper solution. That is, modify thecommandbuiltin to perform variable substitution on its first argument. Then people can do things likecommand $EDITOR $argvandcommand $HOME/bin/mvim.Symmetry argues for modifying the
builtinbuiltin 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.Ericson2314 commentedon Apr 7, 2016
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.
Ericson2314 commentedon Apr 7, 2016
The change to
commandis 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.krader1961 commentedon Apr 8, 2016
See the discussion in issue #564; especially the point made by ridiculousfish on 2013-02-07.
krader1961 commentedon Apr 8, 2016
@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 asfish_title. A stronger argument for indirecting thru a var to a function is to allow callbacks without having to useevalin 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.Ericson2314 commentedon Apr 8, 2016
Yeah to be clear I am 100% against implicit eval / reparsing too. Gross hack, poor security, etc, as you say.
Ericson2314 commentedon Apr 8, 2016
@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$asdfis a huge benefit.Ericson2314 commentedon Apr 8, 2016
@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.
27 remaining items
floam commentedon Jan 6, 2017
I mean, by that token - having a
builtinorcommandstatement 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 afunctions --runor whatever other names have been proposed - it'd ought to do the same thing. What makesexecspecial overcommandwith regards to expansion? I might be missing something obvious?faho commentedon Jan 6, 2017
Same reason
command foois 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,envor whatever. If only some are, then you need to remember which exactly.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 --runorfuncallorcall --functionwould fail anyway, but for consistency it might be nice.faho commentedon Jan 6, 2017
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.
krader1961 commentedon Jan 7, 2017
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.Ericson2314 commentedon Jan 10, 2017
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/callaren't needed, but that's a small point I wouldn't want this to be hung up on!pgan002 commentedon May 28, 2017
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 $varto replacebuiltinandcommand? Or evendo exec $varinstead ofexec? This is slightly more verbose but groups these commands that have quite similar meaning into one command with parameters.faho commentedon May 28, 2017
I can't see how it does. I mean there's
function PAGER; less; endvscall $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"; $fooprints "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).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 tofoo a b c, it would execute afoofunction/builtin/command, but with var set tofunction a b c, it would try to execute a functiona. That seems like a recipe for confusion - if you get a variable from somewhere else, you have to remember to usedo any $varor check against the subcommands yourself. If we instead disallowdo $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
callis clearer thando, which is as generic as it gets, though.Fix call to scrot
cben commentedon Aug 13, 2018
Implicit cd also doesn't work with vars:
Now of course
cd ~/go/src/$PKGis the simplest solution here, zero reason to resort toeval/commandor proposedcall/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.
ridiculousfish commentedon Sep 1, 2018
Revisiting this, I no longer think
doorcallis 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.mqudsi commentedon Oct 18, 2018
@ridiculousfish what about this (overly contrived) example:
(which foo) --barI hit a real-world case today where I would have been forced to use
evalbefore, but thanks to865a464I was able to store the result of the command substitution in a variable then execute that instead, like so:But given that it's equivalent to the (currently illegal) first example, is there a good reason to not enable that as well?
ridiculousfish commentedon Oct 18, 2018
Don't do that, that's what
envis for:env foo --bar...mqudsi commentedon Oct 18, 2018
Of course. Thanks!