What does `$ {1 # * =}` mean in Bash?

I found a great answer on StackOverflow that explains how to pass an associative array to a function. Can anyone ${1#*=}

please help me figure out what the syntax indicates in the code below? (Borrowed from jaypal singh ):

#!/bin/bash

declare -A weapons=(
  ['Straight Sword']=75
  ['Tainted Dagger']=54
  ['Imperial Sword']=90
  ['Edged Shuriken']=25
)

function print_array {
    eval "declare -A arg_array="${1#*=}
    for i in "${!arg_array[@]}"; do
       printf "%s\t%s\n" "$i ==> ${arg_array[$i]}"
    done
}

print_array "$(declare -p weapons)"

      

Here is my guess so far (correct me if I'm wrong on any of them):
- 1

means the first parameter passed to the function ( $1

or ${1}

)
- #

means the index $1

which, since $1

is an associative array, makes the #

keys $1


- *

means the values โ€‹โ€‹of the keys #

in the associated array$1

It leaves =

. What does it mean? Is this as a way to show what you want #

and *

mean the keys and values โ€‹โ€‹of the associated array?

+3


source to share


2 answers


The snippet ${1#*=}

has nothing to do with associative arrays. (Bash syntax is super-consistent and non-confusing) *

This is pattern matching for the value of the first argument ( ${1}

) of your function or script. Its syntax

${variable#glob}

      

Where

  • variable

    - any bash variable
  • glob - any glob pattern (assuming pathname expansion) to match)

It captures the shortest match starting from the beginning of the line. There is also ##

one that captures the longest match starting at the beginning of the variable, %

which captures the shortest match starting at the end, and %%

which captures the longest match starting at the end.


So for example the following code:

myVar="abc=llamas&disclaimer=true"
echo ${myVar#*=}

      



will display abc=

.

On the other hand,

myVar="abc=llamas&disclaimer=true"
echo ${myVar##*=}

      

will print abc=llamas&disclaimer=

and

myVar="foobar is bad"
echo ${myVar%%b*"

      

will print bar is bad


* This is fully explained in the bash man page ; just search for a line ${parameter#word}

to find it

+8


source


It removes the matching string (shortest match from the beginning) with a pattern *=

in the string evaluated with $1

.

$1

is the first positional parameter passed to the shell.

The general format can be written as ${var#patt}

too, where is patt

matched (shortest match from the beginning) to $var

and removed.

Example:

var="first=middle=last"
echo "${var#*=}"

      

Output:

middle=last

      

If instead #

ie ${var##pat}

is used ##

, it pat

is associated for the longest match (from the beginning).



Example:

var="first=middle=last"
echo "${var##*=}"

      

Output:

last

      


From the Bash Manual :

$ {parameter # word}

$ {parameter ## word}

The word is expanded to create a template in the same way as the filename extension (see Filename Extension). If the pattern matches the beginning of an extended parameter value, then the expansion results in an extended parameter value with the shortest match pattern ('# Case) or a long match pattern (case ##). If the parameter is "@ or", the template delete operation is applied to each positional parameter in turn, and the expansion is the result of the list. If the parameter is an array variable with the "@ or" index, the pattern deletion operation is applied to each element of the array in turn, and the expansion is the resulting list.

+4


source







All Articles