Differences between revisions 2 and 3
Revision 2 as of 2018-01-26 21:07:07
Size: 346
Editor: SamatJain
Comment:
Revision 3 as of 2020-02-20 07:04:16
Size: 1276
Editor: SamatJain
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:

== Neat features ==

=== -v for testing set variables, while handling undefined ones ===

-v: True if the shell variable varname is set (has been assigned a value).

bash >=4.2 supports -v for testing for *set* variables while handling empty ones, also no specials needs when running under 'set -u':

{{{#!highlight sh
% cat foo
FOO=''
[ -z "${FOO:-}" ] && echo 1
[ -z "${BAR:-}" ] && echo 2
[ -v FOO ] && echo 3
[ -v BAR ] && echo 4
% bash -u ./foo
1
2
3
}}}

[[https://mobile.twitter.com/mikagrml/status/1222225444558209024?s=19|source]]

also:

If you use bash, you should really use [[ ]] instead of [ ]. It can do everything [ can (and then some, like ERE and glob matches), but is a shell keyword, which improves ergonomics quite a bit. See [ $x = 0 ] vs. [[ $x = 0]] after `unset x`, for example.


== Tools ==

Use shellcheck with timonwong.shellcheck plugin.

Use bash-language-server.

Interesting bash aliases and functions:

Neat features

-v for testing set variables, while handling undefined ones

-v: True if the shell variable varname is set (has been assigned a value).

bash >=4.2 supports -v for testing for *set* variables while handling empty ones, also no specials needs when running under 'set -u':

   1 % cat foo
   2 FOO=''
   3 [ -z "${FOO:-}" ] && echo 1
   4 [ -z "${BAR:-}" ] && echo 2
   5 [ -v FOO ] && echo 3
   6 [ -v BAR ] && echo 4
   7 % bash -u ./foo
   8 1
   9 2
  10 3

source

also:

If you use bash, you should really use ]] instead of [ ]. It can do everything [ can (and then some, like ERE and glob matches), but is a shell keyword, which improves ergonomics quite a bit. See [ $x = 0 ] vs. [[ $x = 0 after unset x, for example.

Tools

Use shellcheck with timonwong.shellcheck plugin.

Use bash-language-server.

SamatsWiki: Bash (last edited 2021-04-25 02:46:19 by SamatJain)