Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

84%
+9 −0
Q&A A shell script that can run under different shells

I'm not familiar with zsh, but it seems to me that your problem here is that the syntax for for loops is different in bash and zsh, which throws bash off as it tries to interpret your script, finds...

posted 3y ago by Canina‭

Answer
#1: Initial revision by user avatar Canina‭ · 2021-12-18T16:05:41Z (almost 3 years ago)
I'm not familiar with zsh, but it seems to me that your problem here is that the syntax for `for` loops is different in bash and zsh, which throws bash off as it tries to interpret your script, finds a keyword it knows but the rest of the statement doesn't match what it expects.

The solution would seem to me to be to put the shell-specific parts in separate files, and only `source` the file matching the current execution environment into the script. So you might have a main script like:

    #!/usr/bin/env bash

    shell=$(ps -p $$ -oargs= | awk '{print $1}')

    if [ $shell = "bash" ]; then
        source ./bash-source.sh
    elif [ $shell = "zsh" ]; then
        source ./zsh-source.sh
    fi

as well as `bash-source.sh`

    for f in ~/.functions.d/*.sh; do source $f; done

and `zsh-source.sh`

    for f (~/.functions.d/**/*.sh) source $f

This way, each shell only sees the `for` statement with the correct syntax for it, and therefore doesn't get tripped up by trying to parse the other. This relies on the fact that at least bash doesn't actually *parse* a file until it's loaded, and loading happens only when the `source` statement is actually *executed*.

If zsh deals gracefully with this situation, you might not need to split the bash source loop into its own file; but clearly bash doesn't like to see the zsh `for` (as evidenced by the fact that you're getting an error relating to it), and thus bash needs a little hand-holding.

Also note that I had to add `| awk '{print $1}'` to the `$shell` assignment. Without it, when within a bash shell script, `$shell` was assigned the whole execution command line; in other words, `bash ./filename.sh` even when executing the script only as `./filename.sh`. There's almost certainly a more efficient way to do that; for the moment, I simply needed something that got me past that assignment and would likely give the intended value. This does however suggest some system-dependence in the `ps` output that you may need to deal with, depending on just how portable you want this script to be.