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

Dashboard
Notifications
Mark all as read
Q&A

A shell script that can run under different shells

+5
−0

I have a shell script with a syntax compatible to both bash and zsh, except for a section that has zsh specific syntax that throws syntax errors if sourced from bash. Is there an easy way to escape such section when using bash?


The script is a bash function that sources all the files in a directory. It works fine from zsh (and it is irrelevant to the question).

#!/usr/bin/env bash

shell=$(ps -p $$ -oargs=)

if [ $shell = "bash" ]; then
	for f in ~/.functions.d/*.sh; do source $f; done
elif [ $shell = "zsh" ]; then
	for f (~/.functions.d/**/*.sh) source $f
fi

The error raised when sourcing it in bash is:

scr: line 8: syntax error near unexpected token `('
scr: line 8: `  for f (~/.functions.d/**/*.sh) source $f'

Relevant links:

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Bash has globstar (1 comment)

1 answer

+5
−0

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.

Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Alternatives to "shell" (1 comment)

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate