From cab8a4c98326dcbafac09950ea08e11597d759c5 Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 1 May 2021 20:56:09 -0700 Subject: [PATCH] Updated SC3044 (markdown) --- SC3044.md | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/SC3044.md b/SC3044.md index 7dcee31..1cdecab 100644 --- a/SC3044.md +++ b/SC3044.md @@ -1 +1,60 @@ -In POSIX sh, 'declare' is undefined. \ No newline at end of file +## In POSIX sh, `declare` is undefined. + +### Problematic code: + +```sh +#!/bin/sh +declare var="value" +``` + +or + +```sh +#!/bin/sh +declare -r readonly +``` + +or + +```sh +#!/bin/sh +declare ... +``` + +### Correct code: + +If assigning a simple variable outside of a function, skip `declare` all together: + +```sh +var=value +``` + +If declaring a variable read-only: +``` +var=value +readonly var +``` + +If you are unable to find a suitable replacement, consider switching to a shell that supports `declare`: + +```sh +#!/bin/bash +declare ... +``` + +Indexed arrays, associative arrays, local variables, namerefs, and integer variables are not supported in POSIX sh. + +### Rationale: + +The `declare` command is non-standard, and most of its functionality is not available across shells. + +Either find a POSIX replacement, or switch to a shell that is guaranteed to support them. + + +### Exceptions: + +If your `declare` command is guarded by a check of the shell version, such as inspecting `$BASH_VERSION`, you can ignore this message. + +### Related resources: + +* Help by adding links to BashFAQ, StackOverflow, man pages, POSIX, etc! \ No newline at end of file