bash括号扩展,用于类似PATH的变量

时间:2018-07-20 20:06:10

标签: bash brace-expansion

我需要设置一个这样的变量:

./configure ... PKG_CONFIG_PATH=/usr/local/opt/libxml2/lib/pkgconfig:/usr/local/opt/imagemagick/lib/pkgconfig:/usr/local/opt/gnutls/lib/pkgconfig

一种显而易见的制作方法是使用Brace Expansion

PKG_CONFIG_PATH=/usr/local/opt/{libxml2,imagemagick,gnutls}/lib/pkgconfig
PKG_CONFIG_PATH=${PKG_CONFIG_PATH// /:}

但是,这不起作用(在变量设置中不会进行括号扩展,并且,当扩展 完成时,分隔符为空格,并且似乎无法改变它。

那么,有没有一种方法可以避免每个软件包的显式前缀和后缀丑陋?

1 个答案:

答案 0 :(得分:3)

假设这是一个脚本,而不是交互使用,我只使用一个数组。

<link href="https://fonts.googleapis.com/css?family=Cookie" rel="stylesheet">

<header>
  <p class=ramly>Ramly</p>
  <image class=logo src="https://upload.wikimedia.org/wikipedia/commons/9/9e/Ramly_Burger_Logo.png"></image>
  <p class=burger>Burger</p>
</header>

<body>
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam vel lacinia magna, vel sodales mi. Praesent dapibus felis non finibus pretium. Nam dui mauris, dictum at nibh ut, vestibulum pharetra ex. Etiam quis faucibus tortor. Pellentesque luctus elementum
  tortor, a molestie tellus luctus fermentum. Quisque vitae tortor quis urna imperdiet vehicula. Maecenas cursus lacus vestibulum metus vestibulum, et vulputate leo rutrum. Vivamus lobortis turpis id commodo lacinia. Integer quis mi risus.
</body>

这时,您可能要考虑是否需要大括号扩展,而只列出目录:

pkg_config_path=(
 /usr/local/opt/{libxml2,imagemagick,gnutls}/lib/pkgconfig
)
./configure ... PKG_CONFIG_PATH=$(IFS=:; printf %s "${pkg_config_path[*]}")