From 7c26f834f908fdc968a7080f0023971d31b9e345 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 27 Feb 2020 15:51:41 -0500 Subject: [PATCH] Project import generated by Copybara. PiperOrigin-RevId: 297665437 --- shellguide.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/shellguide.md b/shellguide.md index 09f8ff5..7769f60 100644 --- a/shellguide.md +++ b/shellguide.md @@ -132,7 +132,7 @@ information is recommended. ```shell err() { - echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" > &2 + echo "[$(date +'%Y-%m-%dT%H:%M:%S%z')]: $*" >&2 } if ! do_something; then @@ -758,6 +758,7 @@ removed `./somefile' `eval` should be avoided. + Eval munges the input when used for assignment to variables and can set variables without making it possible to check what those variables were. @@ -1146,15 +1147,13 @@ my_func2() { ### Function Location Put all functions together in the file just below constants. Don't hide -executable code between functions. +executable code between functions. Doing so makes the code difficult to follow +and results in nasty surprises when debugging. If you've got functions, put them all together near the top of the file. Only includes, `set` statements and setting constants may be done before declaring functions. -Don't hide executable code between functions. Doing so makes the code -difficult to follow and results in nasty surprises when debugging. - ### main @@ -1193,14 +1192,14 @@ Example: ```shell if ! mv "${file_list[@]}" "${dest_dir}/"; then - echo "Unable to move ${file_list[*]} to ${dest_dir}" > &2 + echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2 exit 1 fi # Or mv "${file_list[@]}" "${dest_dir}/" if (( $? != 0 )); then - echo "Unable to move ${file_list[*]} to ${dest_dir}" > &2 + echo "Unable to move ${file_list[*]} to ${dest_dir}" >&2 exit 1 fi ``` @@ -1213,7 +1212,7 @@ following is acceptable: ```shell tar -cf - ./* | ( cd "${dir}" && tar -xf - ) if (( PIPESTATUS[0] != 0 || PIPESTATUS[1] != 0 )); then - echo "Unable to tar files to ${dir}" > &2 + echo "Unable to tar files to ${dir}" >&2 fi ```