From 6b051fa58e7b580886e4957363a50fe4d704c015 Mon Sep 17 00:00:00 2001 From: koalaman Date: Wed, 7 Oct 2015 09:56:59 -0700 Subject: [PATCH] Created SC2105 (markdown) --- SC2105.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 SC2105.md diff --git a/SC2105.md b/SC2105.md new file mode 100644 index 0000000..dfc5913 --- /dev/null +++ b/SC2105.md @@ -0,0 +1,35 @@ +## `break` is only valid in loops + +### Problematic code: + +```sh +case "$1" in + -v) + verbose=1 + break + ;; + -d) + debug=1 +esac +``` + +### Correct code: + +```sh +case "$1" in + -v) + verbose=1 + ;; + -d) + debug=1 +esac +``` +### Rationale: + +`break` or `continue` was found outside a loop. These statements are only valid in loops. In particular, `break` is not required in `case` statements as there is no implicit fall-through. + +To return from a function or sourced script, use `return`. To exit a script, use `exit`. + +### Exceptions: + +It's possible to `break`/`continue` in a function without a loop. The call will then affect the loop -- if any -- that the function is invoked from. This is obviously not good coding practice. \ No newline at end of file