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

"[Command failed: $FZF_DEFAULT_COMMAND]" when reloading and the variable contains spaces #2887

Closed
5 of 10 tasks
matatk opened this issue Jul 22, 2022 · 1 comment
Closed
5 of 10 tasks
Labels

Comments

@matatk
Copy link

matatk commented Jul 22, 2022

  • I have read through the manual page (man fzf)
  • I have the latest version of fzf
  • I have searched through the existing issues

Info

  • OS
    • Linux
    • Mac OS X
    • Windows
    • Etc.
  • Shell
    • bash
    • zsh
    • fish

I found #1830 and #1408 but this seems to be a different issue.

I'm running on macOS with zsh, and fzf 0.31.0 (brew). I have run the install script and sourced ~/.fzf.zsh.

Summary: when using reload() in conjunction with the $FZF_DEFAULT_COMMAND variable, I get an error if the command contains spaces. However, I can use commands that contain spaces inside reload() if I hard code them. I accept this may be a config issue on my end, though I am not aware of having changed any settings that might cause this to break. I also tried running these examples using /bin/sh and had the same problem.

I've just discovered fzf and it looks awesome so far; thanks!


When FZF_DEFAULT_COMMAND contains spaces, I get the "[Command failed: $FZF_DEFAULT_COMMAND]" when I try to reload. The ps example fails with that error message:

FZF_DEFAULT_COMMAND='ps -ef' \
  fzf --bind 'ctrl-r:reload($FZF_DEFAULT_COMMAND)' \
      --header 'Press CTRL-R to reload' --header-lines=1 \
      --height=50% --layout=reverse

But if I take out the -ef part, it works (in the output from ps I can see the time creep up for the fzf command, so it really is re-running the command):

FZF_DEFAULT_COMMAND='ps' \
  fzf --bind 'ctrl-r:reload($FZF_DEFAULT_COMMAND)' \
      --header 'Press CTRL-R to reload' --header-lines=1 \
      --height=50% --layout=reverse

I tried the same thing with ls just to check the problem relates to the space characters in the variable. This causes the "[Command failed: $FZF_DEFAULT_COMMAND]" error on reload:

FZF_DEFAULT_COMMAND='ls -a' \
  fzf --bind 'ctrl-r:reload($FZF_DEFAULT_COMMAND)' \
      --header 'Press CTRL-R to reload' \
      --height=50% --layout=reverse

...but this works:

FZF_DEFAULT_COMMAND='ls' \
  fzf --bind 'ctrl-r:reload($FZF_DEFAULT_COMMAND)' \
      --header 'Press CTRL-R to reload' \
      --height=50% --layout=reverse

In the man page, the invocation example for reload takes the form <key|event>:reload: as opposed to <key|event>:reload() and I'm not entirely clear on why, but trying the colon syntax didn't affect the behaviour. However the docs there referred me to this example, from #1750:

ps -ef | fzf --bind 'ctrl-r:reload(ps -ef)' --header 'Press CTRL-R to reload' \
             --header-lines=1 --layout=reverse

...and this one worked fine! So it must be something to do with variable expansion?

Please let me know if you need any more info.

@junegunn
Copy link
Owner

I'm a bash user, and I didn't realize that the example only works on bash.

# Works
SHELL=bash FZF_DEFAULT_COMMAND='ps -ef' \
  fzf --bind 'ctrl-r:reload($FZF_DEFAULT_COMMAND)' \
      --header 'Press CTRL-R to reload' --header-lines=1 \
      --height=50% --layout=reverse

# Fails
SHELL=zsh FZF_DEFAULT_COMMAND='ps -ef' \
  fzf --bind 'ctrl-r:reload($FZF_DEFAULT_COMMAND)' \
      --header 'Press CTRL-R to reload' --header-lines=1 \
      --height=50% --layout=reverse

So it boils down to

# On bash
SOME_COMMAND='echo foo'
$SOME_COMMAND
  # foo

vs.

# On zsh
SOME_COMMAND='echo foo'
$SOME_COMMAND
  # zsh: command not found: echo foo

The solution is simple; use eval.

eval $SOME_COMMAND
  # foo

So you can do this:

FZF_DEFAULT_COMMAND='ps -ef' \
  fzf --bind 'ctrl-r:reload(eval "$FZF_DEFAULT_COMMAND")' \
      --header 'Press CTRL-R to reload' --header-lines=1 \
      --height=50% --layout=reverse

I'll consider updating the examples.

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