複数行の変数があり、その変数の最初の行だけが必要です。次のスクリプトは問題を示しています。
#!/bin/bash
STRINGTEST="Onlygetthefirstline
butnotthesecond
orthethird"
echo " Take the first line and send to standard output:"
echo ${STRINGTEST%%$'\n'*}
# Output is as follows:
# Onlygetthefirstline
echo " Set the value of the variable to the first line of the variable:"
STRINGTEST=${STRINGTEST%%$'\n'*}
echo " Send the modified variable to standard output:"
echo $STRINGTEST
# Output is as follows:
# Onlygetthefirstline butnotthesecond orthethird
質問:コマンドの${STRINGTEST%%$'\n'*}後に配置すると最初の行を返すechoのに、割り当て後に配置すると改行をスペースに置き換えるのはなぜですか?
$'...'bashの代わりにサポートしていないシェルで実行しようとすると、ユーザーエラーのように聞こえます。