In bash scripting, what’s the different between declare and a normal variable? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of In bash scripting, what’s the different between declare and a normal variable? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

In bash scripting:

we create variable by just naming it:

abc=ok

or we can use declare

declare abc=ok

what’s the difference?

and why does bash make so many ways to create a variable?

Answer

From help -m declare:

NAME

    declare – Set variable values and attributes.

SYNOPSIS

    declare [-aAfFgilnrtux] [-p] [name[=value] …]

DESCRIPTION

    Set variable values and attributes.

    Declare variables and give them attributes. If no NAMEs are given, display the attributes and values of all variables.

    Options:

      -f
        restrict action or display to function names and definitions
      -F
        restrict display to function names only (plus line number and source file when debugging)
      -g
        create global variables when used in a shell function; otherwise ignored
      -p
        display the attributes and value of each NAME

    Options which set attributes:

      -a
        to make NAMEs indexed arrays (if supported)
      -A
        to make NAMEs associative arrays (if supported)
      -i
        to make NAMEs have the ‘integer’ attribute
      -l
        to convert NAMEs to lower case on assignment
      -n
        make NAME a reference to the variable named by its value
      -r
        to make NAMEs readonly
      -t
        to make NAMEs have the ‘trace’ attribute
      -u
        to convert NAMEs to upper case on assignment
      -x
        to make NAMEs export

    Using ‘+’ instead of ‘-’ turns off the given attribute.

    Variables with the integer attribute have arithmetic evaluation (see the let command) performed when the variable is assigned a value.

    When used in a function, declare makes NAMEs local, as with the local command. The ‘-g’ option suppresses this behavior.

    Exit Status:
    Returns success unless an invalid option is supplied or a variable assignment error occurs.

SEE ALSO

    bash(1)

IMPLEMENTATION

    GNU bash, version 4.3.11(1)-release (i686-pc-linux-gnu)
    Copyright (C) 2013 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http&colon;//gnu.org/licenses/gpl.html>

So, declare is used for setting variable values and attributes.

Let me show the use of two attributes with a very simple example:

$ # First Example:
$ declare -r abc=ok
$ echo $abc
ok
$ abc=not-ok
bash: abc: readonly variable


$ # Second Example:
$ declare -i x=10
$ echo $x
10
$ x=ok
$ echo $x
0
$ x=15
$ echo $x
15
$ x=15+5
$ echo $x
20

From the above example, I think you should understand the usage of declare variable over normal variable! This type of declareation is useful in functions, loops with scripting.

Also visit Typing variables: declare or typeset

We are here to answer your question about In bash scripting, what’s the different between declare and a normal variable? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji