Post History
I came up with three methods. Exit early Use exit early instead of the elif syntax. Basically shell is an interpreter. As soon as the parsing of the first if statement is finished, it will be e...
#1: Initial revision
I came up with three methods.
### Exit early
Use `exit` early instead of the `elif` syntax.
Basically shell is an interpreter.
As soon as the parsing of the first `if` statement is finished, it will be executed.
In this case, if shell is bash, `exit` before the parsing of the next `if` statement begins.
```sh
if [ -n "$BASH_VERSION" ]; then
for f in *; do echo __BASH__ $f; done
exit
fi
if [ -n "$ZSH_VERSION" ]; then
for f (*) echo __ZSH__ $f
fi
```
### Eval string
Use `eval`.
```sh
if [ -n "$BASH_VERSION" ]; then
for f in *; do echo __BASH__ "$f"; done
elif [ -n "$ZSH_VERSION" ]; then
eval 'for f (*) echo __ZSH__ "$f"'
fi
```
### Source here-document
This is the same as [Canina's answer](https://linux.codidact.com/posts/285278/285301#answer-285301). However, you can write the code in the same file by using here-document.
```sh
if [ -n "$BASH_VERSION" ]; then
for f in *; do echo __BASH__ "$f"; done
elif [ -n "$ZSH_VERSION" ]; then
source /dev/stdin << '__ZSH_SRC__'
for f (*) echo __ZSH__ "$f"
__ZSH_SRC__
fi
```
