Đầu tiên, tôi sợ rằng giải thích về -otùy chọn được cung cấp bởi http://explainshell.com là không hoàn toàn chính xác.
Cho rằng đó setlà một lệnh bắt nạt, chúng ta có thể xem tài liệu của nó bằng helpcách thực hiện help set:
-o option-name
Set the variable corresponding to option-name:
allexport same as -a
braceexpand same as -B
emacs use an emacs-style line editing interface
errexit same as -e
errtrace same as -E
functrace same as -T
hashall same as -h
histexpand same as -H
history enable command history
ignoreeof the shell will not exit upon reading EOF
interactive-comments
allow comments to appear in interactive commands
keyword same as -k
monitor same as -m
noclobber same as -C
noexec same as -n
noglob same as -f
nolog currently accepted but ignored
notify same as -b
nounset same as -u
onecmd same as -t
physical same as -P
pipefail the return value of a pipeline is the status of
the last command to exit with a non-zero status,
or zero if no command exited with a non-zero status
posix change the behavior of bash where the default
operation differs from the Posix standard to
match the standard
privileged same as -p
verbose same as -v
vi use a vi-style line editing interface
xtrace same as -x
Như bạn có thể thấy -o pipefailcó nghĩa là:
giá trị trả về của một đường ống là trạng thái của lệnh cuối cùng để thoát với trạng thái khác không hoặc bằng 0 nếu không có lệnh nào thoát với trạng thái khác không
Nhưng nó không nói: Write the current settings of the options to standard output in an unspecified format.
Bây giờ, -xđược sử dụng để gỡ lỗi như bạn đã biết và -esẽ ngừng thực thi sau lỗi đầu tiên trong tập lệnh. Hãy xem xét một kịch bản như thế này:
#!/usr/bin/env bash
set -euxo pipefail
echo hi
non-existent-command
echo bye
Các echo byedòng sẽ không bao giờ được thực thi khi -eđược sử dụng vì
non-existent-commandkhông trả lại 0:
+ echo hi
hi
+ non-existent-command
./setx.sh: line 5: non-existent-command: command not found
Nếu không có -edòng cuối cùng sẽ được in vì mặc dù đã xảy ra lỗi, chúng tôi đã không Bashtự động thoát:
+ echo hi
hi
+ non-existent-command
./setx.sh: line 5: non-existent-command: command not found
+ echo bye
bye
set -e thường được đặt ở đầu tập lệnh để đảm bảo rằng tập lệnh sẽ bị dừng khi gặp lỗi đầu tiên - ví dụ: nếu tải xuống tệp không thành công, sẽ không có ý nghĩa để giải nén.
set -uxo pipefail).