Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
adoyle-h committed Sep 13, 2024
1 parent 76b226a commit 44913df
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions shell/bash-pitfalls.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,57 @@ echo "${COMPREPLY[@]}"
或者使用 `shopt -s lastpipe` 开启 lastpipe,可以让管道中最后一个命令运行在当前 shell 环境。(这个方法可能没用)

相关链接 https://stackoverflow.com/questions/36340599/how-does-shopt-s-lastpipe-affect-bash-script-behavior

## set -o errexit -o pipefail 对于管道中的命令无效

```bash
#!/usr/bin/env bash

set -o errexit -o nounset -o pipefail -o errtrace
(shopt -p inherit_errexit &>/dev/null) && shopt -s inherit_errexit

fail() {
return 1
}

foo() {
echo 1
fail
echo 2
}

# echo "Case 1:"
# foo

# echo "Case 2:"
# foo || true

# echo "Case 3:"
# while read -r var; do
# foo || true
# done < <(printf -- 'a\nb\n')
```

取消对应的注释,查看输出。

```
Case 1:
1
```

```
Case 2:
1
2
```


```
Case 3:
1
2
1
2
```

在 Case 2 和 Case 3 中,fail 报错了,但程序依然运行下去。

0 comments on commit 44913df

Please sign in to comment.