Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute Zsh shell commands in Bash Script

Tags:

bash

shell

zsh

I want to execute Zsh function command in Bash script. Here is an example:

~/.zshrc

hello () {
  echo "Hello!"
}

hello.sh

#!/bin/bash
hello

executing above bash script in zsh

(zsh) $ ./hello.sh
hello command not found

I also tried with heredocs:

#!/bin/bash
/bin/zsh - <<'EOF'
  hello
EOF

executing above script with heredocs also says command not found error.

Any suggestions?

Thanks!

like image 680
abhiomkar Avatar asked Nov 12 '15 06:11

abhiomkar


People also ask

Can zsh shell run bash script?

The ZSH shell is an extended version of the Bourne Again Shell; thus, most commands and scripts written for bash will work on ZSH. The ZSH shell provides full programming language features such as variables, control flow, loops, functions, and more.

Are zsh commands the same as Bash?

Zsh has floating-point support that Bash does not possess. Hash data structures are supported in Zsh that are not present in Bash. The invocation features in Bash is better when comparing with Zsh. The prompt look can be controlled in Bash, whereas Zsh is customizable.


1 Answers

You can use it like that :

#!/bin/bash

/bin/zsh -i -c hello

-i : Force shell to be interactive

Then, if the shell is interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc (this is usually your $HOME/.zshrc)

-c : Run a command in this shell

like image 78
bew Avatar answered Sep 19 '22 12:09

bew