ฉันมีตัวแปรหลายบรรทัดและฉันต้องการเพียงบรรทัดแรกในตัวแปรนั้น สคริปต์ต่อไปนี้แสดงให้เห็นถึงปัญหา:
#!/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คำสั่ง แต่แทนที่บรรทัดใหม่ด้วยช่องว่างเมื่อวางหลังจากการกำหนด?
$'...'แทนที่จะทุบตี