Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Bash, should I use declare instead of local and export?

Tags:

bash

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?

like image 709
tompreston Avatar asked Jun 17 '19 08:06

tompreston


People also ask

What does declare in Bash do?

'declare' is a bash built-in command that allows you to update attributes applied to variables within the scope of your shell. In addition, it can be used to declare a variable in longhand. Lastly, it allows you to peek into variables.

What is export used for Bash?

Export is a built-in command of the Bash shell. It is used to mark variables and functions to be passed to child processes. Basically, a variable will be included in child process environments without affecting other environments.

What does it mean to export a variable in Bash?

It means that the shell's child processes do not inherit the shell's variables. The user must export the variables to make them available to child processes. This tutorial will show you how to export Bash variables in Linux using the export command. Access to the terminal/command line. The bash shell.

How do I export an environment variable in Bash?

The easiest way to set environment variables in Bash is to use the “export” keyword followed by the variable name, an equal sign and the value to be assigned to the environment variable.


2 Answers

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.

like image 83
KamilCuk Avatar answered Dec 16 '22 05:12

KamilCuk


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 )

like image 21
Inian Avatar answered Dec 16 '22 06:12

Inian