Bash Array Splice
This week I’ve published a Bash submodule that removes elements from Bash array. Check the documentation for detailed getting started and usage instructions, the source code is available on GitHub.
Bash Variables
_module_name='array-splice'
_module_https_url="https://github.com/bash-utilities/array-splice.git"
_module_base_dir='modules'
_module_path="${_module_base_dir}/${_module_name}"
Bash Submodule Commands
cd "<your-git-project-path>"
mkdir -vp "${_module_base_dir}"
git submodule add --name "${_module_name}"\
-b main\
"${_module_https_url}"\
"${_module_path}"
Write a script that makes use of array_splice
function…
#!/usr/bin/env bash
## Find directory that this script resides in
__SOURCE__="${BASH_SOURCE[0]}"
while [[ -h "${__SOURCE__}" ]]; do
__SOURCE__="$(find "${__SOURCE__}" -type l -ls | sed -n 's@^.* -> \(.*\)@\1@p')"
done
__DIR__="$(cd -P "$(dirname "${__SOURCE__}")" && pwd)"
## Provides array_splice '<list_ref>' '<item>' '<offset>'
source "${__DIR__}/modules/array-splice/array-splice.sh"
list=(
--beginning 'foo'
--middle 'bar'
--end 'spam'
)
printf '${list[*]} -> ( %s )\n' "${list[*]}"
echo "#> array_splice --target 'list' --element '--middle' --offset 1"
array_splice --target 'list' --element '--middle' --offset 1
printf '${list[*]} -> ( %s )\n' "${list[*]}"
## Expected output
#> ${list[*]} -> ( --beginning foo --end spam )