2
  1. In shell, what are the differences between a variable that is declared, defined and set?

    • Does "declared" mean the identifier of the variable exists, but the storage and value of the variable do not?

    • Does "defined" mean both the identifier and the storage of the variable exist?

    • Does "set" mean the value of the variable exists? (I read something about it in the section for shell variables in Bash Manual)

    • Do "defined" and "set" imply each other, and therefore they are the same?

  2. In Bash manual, the section of parameter expansion mentions that ${parameter:-word} does something when parameter is unset or null.

    • Does "null" mean "declared" but not "defined", or "declared" but not "set"?

    • Does "unset" mean not "declared"?

  3. Do the concepts mentioned also apply to the names of functions and mean the same as for parameters/variables?

  4. what other similar concepts do I miss?

Note:

  • I am interested in answers for POSIX shell and Bash respectively.

  • Note that by "the value of a variable exists", I mean the the variable has been assigned a value. By "the storage of a variable exists", I mean the variable has been allocated storage in memory.

  • My questions came from reading https://unix.stackexchange.com/a/56846/674 and related sections in Bash manual.

4
    • "declared" means that you've used declare or typeset to specify the type of the variable. This allows you to specify that the variable names an array, should always be treated as an integer, should be read-only, etc. This doesn't exist in POSIX shell, it's a bash extension.
    • I think "defined" and "set" mean the same thing: a value has been given to the variable. The question you linked to was simply inconsistent in his wording. The POSIX shell specification and bash manual don't use the word "defined" when referring to variables, it just says:

      A parameter is set if it has an assigned value (null is a valid value).

    • "unset" is the opposite of "set" -- the variable has never been assigned (or you've since used unset to remove the assignment). If you use the set command, the variable will not be listed.
    • "null" means the variable has been assigned a null (zero-length) string as the value.
  1. I don't think these terms are relevant for functions. There's no such thing as a null function (you're not allowed to leave the body empty when defining a function). And there's nothing analogous to ${varname:-default} for functions that needs to do anything special if the function isn't defined. So either the function name is defined and it's OK to call it, or it isn't and you'll get an error if you try to call it.

Since shell variables contain arbitrary-length strings, no storage is allocated when the variable is declared. Storage is allocated dynamically when the variable is assigned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.