Skip to content

Instantly share code, notes, and snippets.

@greymd
Last active May 15, 2017 01:11
Show Gist options
  • Save greymd/6d0fbbe58d8583ffefde85f489448434 to your computer and use it in GitHub Desktop.
Save greymd/6d0fbbe58d8583ffefde85f489448434 to your computer and use it in GitHub Desktop.
bash-4.0 here string bug

コマンド置換$(...) の 中に制御構文(ifやwhile)を 入れて更にその中にヒアストリング<<<を使うとbash 4.0.0のみシンタックスエラーが発生する。

  • パッチバージョンが上がった bash 4.0.44ではこのバグは修正されている。
  • ヒアストリングではなくecho(1)などではエラーは発生しない。
  • ヒアドキュメントでは発生しない。

再現

下記のatest.shを用意する。

#!/bin/bash

echo $(
  for i in 1 2 3
  do
    cat <<<"test$i";
  done
)

bash-3.2

$ bash atest.sh
test1 test2 test3

bash-4.0.0

$ bash atest.sh
atest.sh: line 2: unexpected EOF while looking for matching `)'
atest.sh: line 8: シンタックス エラー: 期待してないファイルの終了

bash-4.0.44

$ bash atest.sh
test1 test2 test3

bash-4.3

$ bash atest.sh
test1 test2 test3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment