Sunday, 26 May 2013

How do I stop bash from syntax checking sourced files?

How do I stop bash from syntax checking sourced files?

I have the following code in a file that is sourced by both zsh and bash.
if test $SHELLNAME = zsh ; then
    lss() { l -l ${1:-.}/*(s,S,t); }
    laf() { l ${1:-.}/.*(.); }
    lad() { l -d ${1:-.}/.*(/); }
    lsw() { l -ld ${1:-.}/.*(R,W,X.^ND/); }
fi
SHELLNAME is zsh if the shell is zsh and bash if the shell is bash.
Each of the above functions contains code that works with zsh, but not bash.
When I source this file, I receive the following errors.
bash: <filename>: line 67: syntax error near unexpected token `('
bash: <filename>: line 67: `     lss() { l -l ${1:-.}/*(s,S,t); }'
At the moment, I am using eval to bypass bash's syntax checking.
if test $SHELLNAME = zsh ; then
    # Using eval makes sure bash can't try to syntax-check these functions.
    eval 'lss() { l -l ${1:-.}/*(s,S,t); }'
    eval 'laf() { l ${1:-.}/.*(.); }'
    eval 'lad() { l -d ${1:-.}/.*(/); }'
    eval 'lsw() { l -ld ${1:-.}/.*(R,W,X.^ND/); }'
fi
Is there a better solution to this problem? (perhaps one that doesn't use eval)

No comments:

Post a Comment