View on GitHub

🧰 Utilities.sh

for a Better BASH experience!

download .ZIPdownload .TGZ

_ map

View Source
local ERROR=''

!include assignments/Array

if [ "$1" = '{' ]
then
  !include map/echoCurlyBlock
elif [ "$1" = '[' ]
then
  :
elif [ "$1" = '[[' ]
then
  :
elif [ "$1" = '{{' ]
then
  :
elif [ "$1" = '=~' ]
then
  :
elif [ "$1" = '=' ]
then
  :
elif [[ "$1" = *'_'* ]] || [[ "$1" = *'$#'* ]] || [[ "$1" =~ \$[0-9] ]]
then
  !include map/singleValue
else
  echo "TODO: ARGUMENT ERROR: $# '$*'" >&2
  return 2
fi

Map provided items to given expression.

Arguments

  Type Description  
$@ Array Arguments to map  
$1 String Block String expression or { block

Return Values

  Description
2 Argument error

Example

ls
# => hello.sh world.sh

ls | _ map '${_%.sh}'
# => hello world
View Spec
expect "$( echo "$OUTPUT" | cat -e )" toContain 'specHelper$' 'specHelper.sh$''

Store Results

source $(_ _)

# Save results in an array
_ map results = _:0:1 "Hello" "World"

echo "${#results[@]} results: ${results[*]}"
# => "2 results: H W"
View Spec
expect { example } toEqual "2 results: H W"