Skip to content
Vidar Holen edited this page May 2, 2021 · 3 revisions

In POSIX sh, declare is undefined.

Problematic code:

#!/bin/sh
declare var="value"

or

#!/bin/sh
declare -r readonly

or

#!/bin/sh
declare ...

Correct code:

If assigning a simple variable outside of a function, skip declare all together:

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:

#!/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!

ShellCheck

Each individual ShellCheck warning has its own wiki page like SC1000. Use GitHub Wiki's "Pages" feature above to find a specific one, or see Checks.

Clone this wiki locally