Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm currently trying to run the atom editor in the bash shell, from the fish shell. It's important that I run atom in bash because of how ide-haskell handles ghc-mod path resolution, and a few other standardization issues.

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-atom from fish, I get the following error:

/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!

like image 229
Athan Clark Avatar asked Jan 08 '15 18:01

Athan Clark


People also ask

How do I run a bash script in fish?

Regular bash scripts can be used in fish shell just as scripts written in any language with proper shebang or explicitly using the interpreter (i.e. using bash script.sh ). However, many utilities, such as virtualenv, modify the shell environment and need to be sourced, and therefore cannot be used in fish.

How does fish shell work?

Shells like fish are used by giving them commands. Every fish command follows the same simple syntax. A command is executed by writing the name of the command followed by any arguments. This calls the echo command.

What is the function of bash?

A bash function is a method used in shell scripts to group reusable code blocks. This feature is available for most programming languages, known under different names such as procedures, methods, or subroutines.

Does fish support bash commands?

Fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. Fish supports powerful features like syntax highlighting, autosuggestions, and tab completions that just work, with nothing to learn or configure.


2 Answers

When you run bash file_name it means you're trying to run file_name as a bash script.

Try this instead:

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

The -c means "run this command with bash" instead of "run this script with 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 bash which will use them as positional parameters inside of the supplied command, hence the $@.

like image 162
Mr. Llama Avatar answered Oct 24 '22 23:10

Mr. Llama


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

The underscore will become bash's $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.

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

function start-atom
    bash -l -c '$HOME/lib/atom/bin/Atom/atom "$@"' _ $argv
end
like image 34
glenn jackman Avatar answered Oct 24 '22 23:10

glenn jackman