Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more consistent way to declare Bash variables and functions?

Tags:

bash

shell

I constantly fight with the decision of how to declare a variable or function in Bash.

Given the following assumptions:

  1. Bash is the only scripting language available.
  2. Naming conventions are irrelevant.

In the case of global variables should I use:

  1. foo=bar - inside and outside of functions?
  2. declare -g foo=bar - inside and outside of functions?
  3. local -g foo=bar - inside of functions?

In the case of local variables should I use:

  1. local foo=bar
  2. declare foo=bar

In the case of read-only variables should I use:

  1. declare -r foo=bar
  2. local -r foo=bar
  3. readonly foo - following [1.] or [2.] without the -r flag on the next line.

In the case of functions should I use:

  1. foo() { echo bar; }
  2. foo { echo bar; }
  3. function foo() { echo bar; }
  4. function foo { echo bar; }
like image 516
Tim Friske Avatar asked Nov 04 '22 11:11

Tim Friske


1 Answers

In order to forget about it I define the following near the top of my .bashrc as well as each of my Bash shell script files:

# Allow to define an alias.
#
shopt -s expand_aliases

# Defines a function given a name, empty parentheses and a block of commands enclosed in braces.
#
# @param name the name of the function.
# @param parentheses the empty parentheses. (optional)
# @param commands the block of commands enclosed in braces.
# @return 0 on success, n != 0 on failure.
#
alias def=function

# Defines a value, i.e. read-only variable, given options, a name and an assignment of the form =value.
#
# Viable options:
#   * -i - defines an integer value.
#   * -a - defines an array value with integers as keys.
#   * -A - defines an array value with strings as keys.
#
# @param options the options. (optional)
# @param name the name of the value.
# @param assignment the equals sign followed by the value.
# @return 0 on success, n != 0 on failure.
#
alias val="declare -r"

# Defines a variable given options, a name and an assignment of the form =value.
#
# Viable options:
#   * -i - defines an integer variable.
#   * -a - defines an array variable with integers as keys.
#   * -A - defines an array variable with strings as keys.
#
# @param options the options. (optional)
# @param name the name of the variable.
# @param assignment the equals sign followed by the value. (optional)
# @return 0 on success, n != 0 on failure.
#
alias var=declare

# Declares a function as final, i.e. read-only, given a name.
#
# @param name the name of the function.
# @return 0 on success, n != 0 on failure.
#
alias final="readonly -f"

The above definitions allow me to say for example:

  1. def foo { echo bar; }.
  2. final foo
  3. var foo=bar
  4. val foo=bar

As indicated by the comments you can mix and match various variable flags such as var -g foo=bar for a global (-g) variable (var) or val -Ai foobar=([foo]=0 [bar]=1) for a read-only (val), associative array (-A) consisting of integer (-i) values.

Implicit variable scoping too comes with this approach. Also the newly introduced keywords def, val, var and final should be familiar to any software engineer who programs in languages such as JavaScript, Java, Scala and the like.

like image 86
Tim Friske Avatar answered Nov 08 '22 04:11

Tim Friske