abhiomkar
abhiomkar

Reputation: 5028

How to execute Zsh shell commands in Bash Script

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!

Upvotes: 10

Views: 16802

Answers (1)

bew
bew

Reputation: 503

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

Upvotes: 14

Related Questions