Skip to main content
Bounty Awarded with 100 reputation awarded by CommunityBot
more about arrays and a final hint about 'help declare'
Source Link
sudodus
  • 6.7k
  • 17
  • 27

In most cases it is enough with an implicit declaration in bash

asdf="some text"

But, sometimes you want a variable's value to only be integer (so in case it would later change, even automatically, it could only be changed to an integer, defaults to zero in some cases), and can use:

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed type

or

declare -A asdf   # associative type

You can find good tutorials about arrays in bash when you browse the internet with the search string 'bash array tutorial' (without quotes), for example

linuxconfig.org/how-to-use-arrays-in-bash-script


I think these are the most common cases when you declare variables.

 

Please notice also, that

  • in a function, declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare
    

Finally, you get a brief summary of the features of the shell built-in command declare in bash with the command

help declare

In most cases it is enough with an implicit declaration in bash

asdf="some text"

But, sometimes you want a variable's value to only be integer (so in case it would later change, even automatically, it could only be changed to an integer, defaults to zero in some cases), and can use:

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed type

or

declare -A asdf   # associative type

I think these are the most common cases when you declare variables.

Please notice also, that

  • in a function declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare
    

In most cases it is enough with an implicit declaration in bash

asdf="some text"

But, sometimes you want a variable's value to only be integer (so in case it would later change, even automatically, it could only be changed to an integer, defaults to zero in some cases), and can use:

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed type

or

declare -A asdf   # associative type

You can find good tutorials about arrays in bash when you browse the internet with the search string 'bash array tutorial' (without quotes), for example

linuxconfig.org/how-to-use-arrays-in-bash-script


I think these are the most common cases when you declare variables.

 

Please notice also, that

  • in a function, declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare
    

Finally, you get a brief summary of the features of the shell built-in command declare in bash with the command

help declare
modified to match some points suggested by the original poster
Source Link
sudodus
  • 6.7k
  • 17
  • 27

In most cases it is enough with an implicit declaration in bash

asdf="some text"

SometimesBut, sometimes you want a variablevariable's value to only be integer (so in case it would later change, even automatically, it could only be changed to an integer, defaults to zero in some cases), and can use:

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed type

or

declare -A asdf   # associative type

I think these are the most common cases when you declare variables.

Please notice also, that

  • in a function declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare
    

In most cases it is enough with an implicit declaration

asdf="some text"

Sometimes you want a variable to be an integer, and can use

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed

or

declare -A asdf   # associative

I think these are the most common cases when you declare variables.

Please notice also, that

  • in a function declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare
    

In most cases it is enough with an implicit declaration in bash

asdf="some text"

But, sometimes you want a variable's value to only be integer (so in case it would later change, even automatically, it could only be changed to an integer, defaults to zero in some cases), and can use:

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed type

or

declare -A asdf   # associative type

I think these are the most common cases when you declare variables.

Please notice also, that

  • in a function declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare
    
Source Link
sudodus
  • 6.7k
  • 17
  • 27

In most cases it is enough with an implicit declaration

asdf="some text"

Sometimes you want a variable to be an integer, and can use

declare -i num

or

declare -i num=15

Sometimes you want arrays, and then you need declare

declare -a asdf   # indexed

or

declare -A asdf   # associative

I think these are the most common cases when you declare variables.

Please notice also, that

  • in a function declare makes the variable local (in the function)

  • without any name, it lists all variables (in the active shell)

      declare