14

I've recently discovered the declare Bash builtin, which declares variables with local scope and can also be used to export variables or even set type.

# bar an baz have local scope
foo()
{
    declare bar
    local baz
}

# bing and bong are both exported
declare -x bing
export bong

# i is an integer, j is read-only integer
declare -i i=0
declare  -ri j=10

I've started using it everywhere and stopped using local and export. So why do local and export even exist?

2 Answers 2

19

They exists because of the history. This manual says declare was introduced in bash version 2, local was introduced earlier. People use local, export and readonly by convention and readability.

When I see local I think - 'Och! This must be a function local variable. I must be reading a function.'. When I see declare I need to scroll up until I see a function declaration, to know if i'm in a function and to see if the variable is local.

With export, the export is a POSIX builtin, so it will work everywhere, which is important. But it's similar. When I see export I think - 'Och! This variable is getting exported!'. When I see declare -x I need to refresh my memory with declare options (well, it's easy, -x sounds like export, but it's still one more thing to remember). I prefer writing local and export. Because it is they way my mind thinks - this variable is local, that variable is exported.

When I read scripts with only local and export, without the use of declare, I know it's an easy script. declare -i or declare -n can complicate things.

Also, typeset and declare are exact synonyms. So, you could also ask, why declare, when you can typeset? Probably typeset was introduced, so you can run ksh scripts using bash without any changes. Same with local and readonly keywords. Similar with mapfile and readarray. Convention. With a file you could go with mapfile, but with a here string I sometimes go with readarray, because I am reading some data into an array, and not mapping the file.

I believe the local keyword is (a bit?) more portable then declare. You could read ex. this unix.stackexchange thread for some more info.

Sign up to request clarification or add additional context in comments.

Comments

7

In most cases Yes! but remember using declare in most cases with few of its flags causes the shell to interpret them as expressions rather than just plain assignment strings. Assume you want to define a variable to hold integer only values with the -i attribute

declare -i x; x=2+2; echo $x
4
export x; x=2+2; echo $x
2+2

The shell has forced the assignment to be treated as a integer expression instead of a plain variable assignment. The export/local commands don't define this special processing to be applied to assignments.

Also the built-ins local/export were added to bash much earlier than the attributes which declare provides. POSIX does not define either local or declare, so if you were to target scripts written on a minimal sh shell, only export is available ( note that exporting a function with -f is still only Bash-ism and not POSIX )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.