Saya memiliki variabel multiline, dan saya hanya ingin baris pertama dalam variabel itu. Script berikut menunjukkan masalah:
#!/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
Pertanyaan: Mengapa ${STRINGTEST%%$'\n'*}mengembalikan baris pertama ketika ditempatkan setelah echoperintah, tetapi mengganti baris baru dengan spasi ketika ditempatkan setelah penugasan?
$'...'bukan bash.