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?

pseyfert
  • 838
  • 7
  • 20

1 Answers1

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.

pseyfert
  • 838
  • 7
  • 20
  • 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. – Stéphane Chazelas Oct 25 '16 at 22:20
  • See the dup Q&A for [the zsh equivalent of bash's `${!var}`](/a/119442) or the [proper way to use `eval`](/a/68271) – Stéphane Chazelas Oct 25 '16 at 22:23