From 1e70f54ccdcd9b6a1aea1230dadc2539cc42b0f1 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 21 Jul 2018 15:04:15 +0200 Subject: Update bash style guide --- doc/bash-style.cli | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/doc/bash-style.cli b/doc/bash-style.cli index 93cf9e0..f81479b 100644 --- a/doc/bash-style.cli +++ b/doc/bash-style.cli @@ -54,7 +54,8 @@ done \ For \c{if} use \c{[ ]} for basic tests and \c{[[ ]]} only if the previous form -is not sufficient. Never use \c{test}. Do use \c{elif}. +is not sufficient. Use \c{test} for filesystem tests (presence of files, +etc). Do use \c{elif}. \h1#struct|Structure| @@ -282,12 +283,42 @@ if ! grep \"foo\" /tmp/bar; then fi \ +Note that the \c{if}-condition can be combined with capturing the output, for +example: + +\ +if v=\"$(...)\"; then + ... +fi +\ + If you need to ignore the exit status, you can use \c{|| true}, for example: \ foo || true \ + +\h1#bool|Boolean| + +For boolean values use empty for false and \c{true} for true. This way you +can have terse and natural looking conditions, for example: + +\ +first=true +while ...; do + + if [ ! \"$first\" ]; then + ... + fi + + if [ \"$first\" ]; then + first= + fi + +done +\ + \h1#function|Functions| If a function takes arguments, provide a brief usage after the function -- cgit v1.1