Skip to main content
added 52 characters in body
Source Link

You can do this echo "${!var3}" trick in place of using eval, whichsince eval is considered "dangerous" and "evil". Here's the equivalent way to do this withFor understanding and education though, eval insteadhere is the equivalent way to do this with eval instead:

You can do this echo "${!var3}" trick in place of using eval, which is considered "dangerous" and "evil". Here's the equivalent way to do this with eval instead:

You can do this echo "${!var3}" trick in place of using eval, since eval is considered "dangerous" and "evil". For understanding and education though, here is the equivalent way to do this with eval instead:

added 495 characters in body
Source Link
  1. Note that the need for this ! trick can frequently be avoided using associative arrays in bash. Associative arrays are essentially "hash tables", which are called "unordered maps" in C++ and "dicts" (dictionaries) in Python. Here are a few relevant links on them:

    1. https://stackoverflow.com/questions/6149679/multidimensional-associative-arrays-in-bash
    2. [VERY GOOD TUTORIAL!] *****https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/ Keep in mind, however, that associative arrays (and all other arrays too) in bash are 1-dimensional! From man 1 bash (emphasis added):

    Keep in mind, however, that associative arrays (and all other arrays too) in bash are 1-dimensional! From man 1 bash (emphasis added):

    Arrays Bash

    Bash provides one-dimensional indexed and associative array variables. Any variable may be used as an indexed array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions) and are zero-based; associative arrays are referenced using arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative integers.

    An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value. The subscript is treated as an arithmetic expression that must eval‐ uate to a number. To explicitly declare an indexed array, use declare -a name (see SHELL BUILTIN COMMANDS below). declare -a name[subscript] is also accepted; the subscript is ignored.

    Associative arrays are created using declare -A name.

    Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.

    Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. Indexed array assignments do not require anything but string. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.

    When assigning to an associative array, the subscript is required.

  1. Note that the need for this ! trick can frequently be avoided using associative arrays in bash. Associative arrays are essentially "hash tables", which are called "unordered maps" in C++ and "dicts" (dictionaries) in Python. Here are a few relevant links on them:

    1. https://stackoverflow.com/questions/6149679/multidimensional-associative-arrays-in-bash
    2. [VERY GOOD TUTORIAL!] *****https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/ Keep in mind, however, that associative arrays (and all other arrays too) in bash are 1-dimensional! From man 1 bash (emphasis added):

    Arrays Bash provides one-dimensional indexed and associative array variables. Any variable may be used as an indexed array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions) and are zero-based; associative arrays are referenced using arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative integers.

    An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value. The subscript is treated as an arithmetic expression that must eval‐ uate to a number. To explicitly declare an indexed array, use declare -a name (see SHELL BUILTIN COMMANDS below). declare -a name[subscript] is also accepted; the subscript is ignored.

    Associative arrays are created using declare -A name.

    Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.

    Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. Indexed array assignments do not require anything but string. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.

    When assigning to an associative array, the subscript is required.

  1. Note that the need for this ! trick can frequently be avoided using associative arrays in bash. Associative arrays are essentially "hash tables", which are called "unordered maps" in C++ and "dicts" (dictionaries) in Python. Here are a few relevant links on them:

    1. https://stackoverflow.com/questions/6149679/multidimensional-associative-arrays-in-bash
    2. [VERY GOOD TUTORIAL!] *****https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/

    Keep in mind, however, that associative arrays (and all other arrays too) in bash are 1-dimensional! From man 1 bash (emphasis added):

    Arrays

    Bash provides one-dimensional indexed and associative array variables. Any variable may be used as an indexed array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions) and are zero-based; associative arrays are referenced using arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative integers.

    An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value. The subscript is treated as an arithmetic expression that must eval‐ uate to a number. To explicitly declare an indexed array, use declare -a name (see SHELL BUILTIN COMMANDS below). declare -a name[subscript] is also accepted; the subscript is ignored.

    Associative arrays are created using declare -A name.

    Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.

    Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. Indexed array assignments do not require anything but string. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.

    When assigning to an associative array, the subscript is required.

added 495 characters in body
Source Link

Related

  1. Note that the need for this ! trick can frequently be avoided using associative arrays in bash. Associative arrays are essentially "hash tables", which are called "unordered maps" in C++ and "dicts" (dictionaries) in Python. Here are a few relevant links on them:

    1. https://stackoverflow.com/questions/6149679/multidimensional-associative-arrays-in-bash
    2. [VERY GOOD TUTORIAL!] *****https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/ Keep in mind, however, that associative arrays (and all other arrays too) in bash are 1-dimensional! From man 1 bash (emphasis added):

    Arrays Bash provides one-dimensional indexed and associative array variables. Any variable may be used as an indexed array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions) and are zero-based; associative arrays are referenced using arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative integers.

    An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value. The subscript is treated as an arithmetic expression that must eval‐ uate to a number. To explicitly declare an indexed array, use declare -a name (see SHELL BUILTIN COMMANDS below). declare -a name[subscript] is also accepted; the subscript is ignored.

    Associative arrays are created using declare -A name.

    Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.

    Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. Indexed array assignments do not require anything but string. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.

    When assigning to an associative array, the subscript is required.

Related

  1. Note that the need for this ! trick can frequently be avoided using associative arrays in bash. Associative arrays are essentially "hash tables", which are called "unordered maps" in C++ and "dicts" (dictionaries) in Python. Here are a few relevant links on them:

    1. https://stackoverflow.com/questions/6149679/multidimensional-associative-arrays-in-bash
    2. [VERY GOOD TUTORIAL!] *****https://www.artificialworlds.net/blog/2012/10/17/bash-associative-array-examples/ Keep in mind, however, that associative arrays (and all other arrays too) in bash are 1-dimensional! From man 1 bash (emphasis added):

    Arrays Bash provides one-dimensional indexed and associative array variables. Any variable may be used as an indexed array; the declare builtin will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Indexed arrays are referenced using integers (including arithmetic expressions) and are zero-based; associative arrays are referenced using arbitrary strings. Unless otherwise noted, indexed array indices must be non-negative integers.

    An indexed array is created automatically if any variable is assigned to using the syntax name[subscript]=value. The subscript is treated as an arithmetic expression that must eval‐ uate to a number. To explicitly declare an indexed array, use declare -a name (see SHELL BUILTIN COMMANDS below). declare -a name[subscript] is also accepted; the subscript is ignored.

    Associative arrays are created using declare -A name.

    Attributes may be specified for an array variable using the declare and readonly builtins. Each attribute applies to all members of an array.

    Arrays are assigned to using compound assignments of the form name=(value1 ... valuen), where each value is of the form [subscript]=string. Indexed array assignments do not require anything but string. When assigning to indexed arrays, if the optional brackets and subscript are supplied, that index is assigned to; otherwise the index of the element assigned is the last index assigned to by the statement plus one. Indexing starts at zero.

    When assigning to an associative array, the subscript is required.

added 152 characters in body
Source Link
Loading
added 152 characters in body
Source Link
Loading
added 152 characters in body
Source Link
Loading
Source Link
Loading