/
hubbitus
/
shell.scripts
Обзор
Документация
Войти
/
hubbitus
/
shell.scripts
Код
Запросы
0
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
SHARED/array_join.bash
30 строк
963 B
Pavel Alexeev
function join_by
01 янв 2020, 16:34
01 янв 2020, 16:34
9bda5b5
Код
Авторство
О чём код?
#!/bin/bash # Function to join array by separator. # $1 - separator # $2 - array (declare # Compilation of recipes from http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array # Safe for arrays with elements having spaces and multi-symbol separators (unlike solutions on IFS changing) function array_join() { # http://www.linuxquestions.org/questions/programming-9/bash-passing-arrays-with-spaces-611159/#post3012084 sep=$1 shift #printf "%s\n" "$@" IFS=; res="${*/#/$sep}" echo ${res:${#sep}} } # Example of call: # foo=('foo bar' 'foo baz' 'bar baz') # array_join '<!>' "${foo[@]}" # Res: foo bar<!>foo baz<!>bar baz # Or just: # array_join '<!>' 'foo bar' 'foo baz' 'bar baz' # Res: foo bar<!>foo baz<!>bar baz # Most universal one. # By https://stackoverflow.com/questions/1527049/how-can-i-join-elements-of-an-array-in-bash/17841619#17841619 function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; }