From 7341de17e49560fa0a308e42a6383d41c63ce06e Mon Sep 17 00:00:00 2001 From: austinbutler Date: Wed, 6 Jul 2022 16:23:29 -0700 Subject: [PATCH] Revert 9cfa9fdb2964a11b58151b22dff9cbb2e920e9d6...cac27831e1fd29f27821133b6eaebb30df70550a on SC2154 --- SC2154.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/SC2154.md b/SC2154.md index 19bff74..4492210 100644 --- a/SC2154.md +++ b/SC2154.md @@ -1,6 +1,63 @@ -RUN \ - export http_proxy=${http_proxy}; \ - export https_proxy=${https_proxy}; \ - dnf -y upgrade; \ - dnf -y install libXext libXrender libXtst xorg-x11-server-Xvfb fontconfig unzip sudo telnet; \ - dnf -y clean all \ No newline at end of file +## var is referenced but not assigned. + +### Problematic code: + +```sh +var=name +n=42 +echo "$var_$n.jpg" # overextended +``` + +or + +```sh +target="world" +echo "hello $tagret" # misspelled +``` + +or + +```sh +echo "Result: ${mycmd -a myfile}" # trying to execute commands +``` + +### Correct code: + +```sh +var=name +n=42 +echo "${var}_${n}.jpg" +``` + +or + +```sh +target="world" +echo "hello $target" +``` + +or + +```sh +echo "Result: $(mycmd -a myfile)" +``` + +### Rationale: + +ShellCheck has noticed that you reference a variable that is not assigned. Double check that the variable is indeed assigned, and that the name is not misspelled. + +Note: This message only triggers for variables with lowercase characters in their name (`foo` and `kFOO` but not `FOO`) due to the standard convention of using lowercase variable names for unexported, local variables. + +### Exceptions: + +ShellCheck intentionally does not attempt to figure out runtime or dynamic assignments like with `source "$(date +%F).sh"` or `eval var=value`. See [[SC2034]] for an extended discussion of why this is the case. + +If you know for a fact that the variable is set, you can use `${var:?}` to fail if the variable is unset (or empty), initialize it to a default value if uninitialized with `: "${var:=}"`, or explicitly initialize/declare it with `var=""` or `declare var`. You can also disable the message with a [[directive]]. + +--- + +## Read more: + +POSIX - Parameter expansion: +* https://stackoverflow.com/a/16753536/2309247 +* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02 \ No newline at end of file