0

I just stumbled over a script with works in bash but not in zsh:

if [ Darwin = `uname` ]; then library_path=DYLD_LIBRARY_PATH else library_path=LD_LIBRARY_PATH fi if [ -z "${!library_path}" ]; then eval ${library_path}=${thisdir}/lib64:${thisdir}/lib; export ${library_path} else eval ${library_path}=${thisdir}/lib64:${thisdir}/lib:${!library_path}; export ${library_path} fi 

the second if should apparently test, depending on the kernel, if $LD_LIBRARY_PATH is non zero, or if $DYLD_LIBRARY_PATH is non zero. And then either set or expand the respective variable.

How would I do the same in zsh? And is there a version that works in zsh and bash?

1

1 Answer 1

0

It appears $(eval echo \$\{$library_path\}) works:

if [ Darwin = `uname` ]; then library_path=DYLD_LIBRARY_PATH else library_path=LD_LIBRARY_PATH fi if [ -z $(eval echo \$\{$library_path\}) ]; then eval ${library_path}=${thisdir}/lib64:${thisdir}/lib; export ${library_path} else eval ${library_path}=${thisdir}/lib64:${thisdir}/lib:$(eval echo \$\{$library_path\}); export ${library_path} fi 

though there might be a more elegant solution.

2
  • That's wrong (and potentially dangerous) as you're also evaluating the content of $thisdir as shell code as well . You also don't need to use echo or process substitution (especially unquoted!) here. Commented Oct 25, 2016 at 22:20
  • See the dup Q&A for the zsh equivalent of bash's ${!var} or the proper way to use eval Commented Oct 25, 2016 at 22:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.