mirror of
https://github.com/dylanaraps/pure-sh-bible
synced 2024-05-17 21:06:36 +02:00
Add section for testing file descriptor 1
This commit is contained in:
parent
2d2d96f63b
commit
c40246c9da
24
README.md
24
README.md
|
@ -82,6 +82,7 @@ See something incorrectly described, buggy or outright wrong? Open an issue or s
|
|||
* [Get the current working directory](#get-the-current-working-directory)
|
||||
* [Get the PID of the current shell](#get-the-pid-of-the-current-shell)
|
||||
* [Get the current shell options](#get-the-current-shell-options)
|
||||
* [Check if output is to a terminal](#check-if-output-is-to-a-terminal)
|
||||
* [AFTERWORD](#afterword)
|
||||
|
||||
<!-- vim-markdown-toc -->
|
||||
|
@ -1003,6 +1004,29 @@ This is an alternative to the `pwd` built-in.
|
|||
"$-"
|
||||
```
|
||||
|
||||
## Check if output is to a terminal
|
||||
|
||||
You can test whether file descriptor 1 (i.e. standard output) is open
|
||||
on a terminal using `[ -t 1 ]`. This test fails if output is
|
||||
redirected to a file (`>file`) or a pipe (`|cmd`).
|
||||
|
||||
```shell
|
||||
if [ -t 1 ]; then
|
||||
echo terminal
|
||||
else
|
||||
echo not a terminal
|
||||
fi
|
||||
```
|
||||
|
||||
**Example usage:**
|
||||
|
||||
```shell
|
||||
# show output in color when called interactively
|
||||
[ -t 1 ] && printf '\033[38;5;1m'
|
||||
echo hey
|
||||
[ -t 1 ] && printf '\033[m'
|
||||
```
|
||||
|
||||
# AFTERWORD
|
||||
|
||||
Thanks for reading! If this bible helped you in any way and you'd like to give back, consider donating. Donations give me the time to make this the best resource possible. Can't donate? That's OK, star the repo and share it with your friends!
|
||||
|
|
Loading…
Reference in a new issue