Author Topic: ความแตกต่างระหว่าง && และ ; ในการรัน shell script  (Read 3958 times)

golfreeze

  • Administrator
  • Hero Member
  • *****
  • Posts: 2140
    • View Profile
    • นั่งสมาธิ สติปัฏฐานสี่ พาเที่ยววัด แนะนำวัด แจกcd ธรรมะฟรี
    • Email
In the shell, && and ; are similar in that they both can be used to terminate commands.
The difference is && is also a conditional operator. With ; the following command is always executed, but with && the later command is only executed if the first succeeds.

false; echo "yes"   # prints "yes"
true; echo "yes"    # prints "yes"
false && echo "yes" # does not echo
true && echo "yes"  # prints "yes"
Newlines are interchangeable with ; when terminating commands.

สรุปคือ ถ้าใช้ && ขั้น คือต้องรัน command แรกเสร็จแล้วค่อยรันอีก command ต่อ เช่น
true && echo "yes"  # prints "yes"

แต่ถ้า ; ก็คือไม่สนใจว่า ประโยคข้างหน้าจะจริงหรือเปล่าให้ รัน command ที่สองเลย