All Posts programming declare variables and give them attributes | GNU utils

declare variables and give them attributes | GNU utils

· 150 words · 1 minute read

Declare variables and give them attributes.

Declare a string variable with the specified value:

declare variable="value"

Declare an integer variable with the specified value:

declare -i variable="value"

Declare an array variable with the specified value:

declare -a variable=(item_a item_b item_c)

Declare an associative array variable with the specified value:

declare -A variable=([key_a]=item_a [key_b]=item_b [key_c]=item_c)

Declare a readonly string variable with the specified value:

declare -r variable="value"

Declare a global variable within a function with the specified value:

declare -g variable="value"

Print a function definition:

declare -f function_name

For more information, check out the documentation: https://www.gnu.org/software/bash/manual/bash.html#index-declare .

I hope you enjoyed reading this post as much as I enjoyed writing it. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , and GitHub .