Fish shell 和通过 `function` 从 bash 执行程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27847300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:09:41  来源:igfitidea点击:

The fish shell and executing programs from bash through `function`

bashexecutablefish

提问by Athan Clark

I'm currently trying to run the atom editorin the bashshell, from the fishshell. It's important that I run atomin bashbecause of how ide-haskell handles ghc-modpath resolution, and a few other standardization issues.

我目前正试图运行 原子编辑器bashfish。由于 ide-haskell 如何处理路径解析以及其他一些标准化问题,我atom参与其中很重要。bashghc-mod

Here is how I was going at it:

这是我的处理方式:

#~/.config/fish/config.fish

function start-atom
  bash $HOME/lib/atom/bin/Atom/atom $argv
end

However, when I try running start-atomfrom fish, I get the following error:

但是,当我尝试start-atom从运行时fish,出现以下错误:

/home/athan/lib/atom/bin/Atom/atom: /home/athan/lib/atom/bin/Atom/atom: cannot execute binary file

Even though I know this file is correct and executable. Any ideas? Thank you!

即使我知道这个文件是正确且可执行的。有任何想法吗?谢谢!

回答by Mr. Llama

When you run bash file_nameit means you're trying to run file_nameas a bash script.

当您运行时,bash file_name这意味着您正在尝试file_name作为 bash 脚本运行。

Try this instead:

试试这个:

bash -c '$HOME/lib/atom/bin/Atom/atom "$@"' dummy $argv

The -cmeans "run this commandwith bash" instead of "run this script with bash".

-c意思是“运行此命令在bash”,而不是“使用bash运行此脚本”。

As Charles pointed out in the comments, we have to do a bit of tweaking to pass the parameters to the command. We pass them to bashwhich will use them as positional parameters inside of the supplied command, hence the $@.

正如查尔斯在评论中指出的那样,我们必须做一些调整才能将参数传递给命令。我们将它们传递给它们bash,将它们用作所提供命令内的位置参数,因此$@.

回答by glenn Hymanman

should be: bash -c '$HOME/lib/atom/bin/Atom/atom "$@"' _ $argv

应该: bash -c '$HOME/lib/atom/bin/Atom/atom "$@"' _ $argv

The underscore will become bash's $0

下划线将成为 bash 的 $0

A demo:

一个演示:

$ function test_bash_args
      bash -c 'printf "%s\n" "$@"' _ $argv
  end
$ test_bash_args one two three
one
two
three

If you need that bash session to load your configs, make it a login shell.

如果您需要该 bash 会话来加载您的配置,请将其设为登录 shell。

So, bottom line: ~/.config/fish/functions/start-atom.fish

所以,底线: ~/.config/fish/functions/start-atom.fish

function start-atom
    bash -l -c '$HOME/lib/atom/bin/Atom/atom "$@"' _ $argv
end