[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Savannah-cvs] [800] bash style guide: set -u can be useful
From: |
iank |
Subject: |
[Savannah-cvs] [800] bash style guide: set -u can be useful |
Date: |
Thu, 22 Aug 2024 05:54:53 -0400 (EDT) |
Revision: 800
http://svn.savannah.gnu.org/viewvc/?view=rev&root=administration&revision=800
Author: iank
Date: 2024-08-22 05:54:52 -0400 (Thu, 22 Aug 2024)
Log Message:
-----------
bash style guide: set -u can be useful
Modified Paths:
--------------
trunk/sviki/fsf/bash-style-guide.mdwn
Modified: trunk/sviki/fsf/bash-style-guide.mdwn
===================================================================
--- trunk/sviki/fsf/bash-style-guide.mdwn 2024-07-24 17:50:09 UTC (rev
799)
+++ trunk/sviki/fsf/bash-style-guide.mdwn 2024-08-22 09:54:52 UTC (rev
800)
@@ -451,11 +451,17 @@
rm -v ./* # a file could be named -rf
```
-Avoid `set -u`. Empty vars are useful. Check for them with `\[[ $var ]]`. `set
--u` only leads to explicitly setting variables to empty, and other hackery
-to avoid unset errors - a complication which is not worth the trouble.
-`shellcheck` will find completely unset variables.
+Check for empty & unset variables with `\[[ $var ]]`.
+
+Optionally use `set -u` when using automatic error handling. It can
+prevent errors in somewhat complicated scripts where you set variables
+within conditionals. It also makes code verbose because unset/empty
+variables are often useful and so then you need to set variables to
+empty, eg `x=`, complicating your code and potentially also leading to
+bugs. Avoid relying on set -u. If you see a situation where you think
+set -u might catch an error, add an explicit test for an empty variable.
+
Prefer `$var` except when `${var}` is necessary or adds clarity, like
`"comb${x}ining strings"`.
Never use `[`. `[[` is a special syntax which doesn't do variable expansion
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Savannah-cvs] [800] bash style guide: set -u can be useful,
iank <=