zsh 相当于 bash 的 ${!variable}

zsh 相当于 bash 的 ${!variable}

我刚刚偶然发现了一个在 bash 中有效但在 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

第二个 if 显然应该根据内核来测试 if$LD_LIBRARY_PATH是否非零,或者 if$DYLD_LIBRARY_PATH是否非零。然后设置或扩展相应的变量。

我如何在 zsh 中做同样的事情?有没有可以在 zsh 和 bash 中运行的版本?

答案1

看起来$(eval echo \$\{$library_path\})有效:

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

尽管可能有更优雅的解决方案。

相关内容