41votes
Accepted
How to remove new line added by readarray when using a delimiter?
The implicit trailing new-line character is not added by the readarray builtin, but by the here-string (<<<) of bash, see Why does a bash here-string add a trailing newline char?. You can get ...
32votes
Bash - reverse an array
Another unconventional approach: #!/bin/bash array=(1 2 3 4 5 6 7) f() { array=("${BASH_ARGV[@]}"); } shopt -s extdebug f "${array[@]}" shopt -u extdebug echo "${array[@]}" Output: 7 6 5 4 3 2 1 ...
26votes
Bash - reverse an array
Unconventional approach (all not pure bash): if all elements in an array are just one characters (like in the question) you can use rev: echo "${array[@]}" | rev otherwise if none of the ...
20votes
Accepted
Bash - reverse an array
I have answered the question as written, and this code reverses the array. (Printing the elements in reverse order without reversing the array is just a for loop counting down from the last element to ...
19votes
Accepted
Run a command using arguments that come from an array
Giving the arguments from an array is easy, "${array[@]}" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first ...
16votes
Bash - reverse an array
If you actually want the reverse in another array: reverse() { # first argument is the array to reverse # second is the output array declare -n arr="$1" rev="$2" for i in "${arr[@]}" ...
12votes
Accepted
Why is "${ARRAY[@]}" expanded into multiple words, when it's quoted?
Because arrays when indexed with @ and double quoted expand to a list of the elements. It's documented in man bash under "Arrays": If the word is double-quoted, ... ${name[@]} expands ...
10votes
Accepted
Bash's read builtin errors on a string-based timeout option specification but not an array-based one. Why?
The reason is a difference in how the read builtin function and the date command interpret their command-line arguments. But, first things first. In both of your examples, you place - as is ...
10votes
Accepted
Replace prefix string from lines in a file, and put into a bash array
Read the original names into an array: readarray -t names <groupAfiles.txt Replace the initial file prefix with /dev/loop: names=( "${names[@]/#file//dev/loop}" ) The substitution used ...
9votes
Accepted
What is the difference between ${array[*]} and ${array[@]}? When use each one over the other?
Compare the output of these three loops: #!/bin/bash declare -a arr=("this is" "a test" "of bash") echo "LOOP 1" for x in ${arr[*]}; do echo "item: $x&...
8votes
Accepted
How to pipe multiple results into a command?
aws efs describe-mount-targets --file-system-id ${SharedFileSystem} \ | jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts or, if you prefer, aws efs ...
7votes
Accepted
Find array length in zsh script
${#*[@]} would be the length of the $* array also known as $@ or $argv, which is the array of positional parameters (in the case of a script or function, that's the arguments the script or function ...
7votes
Bash - reverse an array
Ugly, unmaintainable, but one-liner: eval eval echo "'\"\${array['{$((${#array[@]}-1))..0}']}\"'"
7votes
Accepted
Why does printing an array with @ using printf in bash only print the first element?
printf interprets its first argument as a format string, and prints that; any further arguments are only used as required in the format string. With printf "${snapshots[*]}\n", the first ...
6votes
Run a command using arguments that come from an array
tabs=("-t" "one tab" "-t" "second tab") echo app "${tabs[@]}" app -t one tab -t second tab So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.
6votes
Bash - reverse an array
Pure bash solution, would work as a one-liner. $: for (( i=${#array[@]}-1; i>=0; i-- )) > do rev[${#rev[@]}]=${array[i]} > done $: echo "${rev[@]}" 7 6 5 4 3 2 1
6votes
Bash - reverse an array
To reverse an arbitrary array (which may contain any number of elements with any values): With zsh: array_reversed=("${(@Oa)array}") With bash 4.4+, given that bash variables can't contain ...
6votes
Accepted
How to access further members of an array when using bash variable indirection?
"${!x[1]}" is an indirect reference using the element at index 1 of the array x. $ foo=123; bar=456; x=(foo bar); echo "${!x[1]}" 456 In current versions of Bash (4.3 and above), you can use namerefs ...
6votes
Accepted
How to pass an array as function argument but with other extra parameters?
It is not possible to pass an array as argument like that. Even though it looks like you do that, it does not work as you expect it Your shell (e.g. here: bash) will expand "${array[@]}" to ...
6votes
Accepted
Bash 4.4 local readonly array variable scoping: bug?
Your script runs correctly with release 5.1 of the bash shell, but not with intermediate releases after 4.3. The bug or bugs might have been introduced around release 4.3 or 4.4. Multiple changes ...
6votes
Accepted
How do I select an array to loop through from an array of arrays?
You'll store the array names in aoarrs, and inside the select body declare a nameref to the chosen name: ARGENT=("Nous devons économiser de l'argent." "Je dois économiser de l'argent.&...
6votes
Accepted
How can I take a sub-array in bash of the first N elements of a string array with elements containing spaces?
You are assigning the array slice to a non-array variable, so you are getting a string. If you instead assign to an array, and quote it to protect from split+glob, it will work as you expect: $ arr=(&...
5votes
gnu parallel with bash array
If the ids fits on a single command line: parallel --jobs 28 recon-all -s {.} -all -qcache ::: "${ids[@]}" Else like Lucas suggests: printf %s\\n "${ids[@]}" | parallel --jobs 28 ...
5votes
Accepted
Find second largest value in array
printf '%s\n' "${array[@]}" | sort -n | tail -2 | head -1 Print each value of the array on it's own line, sort it, get the last 2 values, remove the last value secondGreatest=$(printf '%s\n' "${array[...
5votes
Find array length in zsh script
files=(*) printf 'There are %d files\n' "${#files[@]}" or set -- * printf 'There are %d files\n' "$#" You have to name the array first (as I did above with files) or use the built-in array $@ by ...
5votes
How to remove new line added by readarray when using a delimiter?
You could use split+glob (what happens when you leave an expansion unquoted in list contexts). It gets in our way most of the time, it would be a shame not to use it when we actually need it: IFS=, ...
5votes
How do I split a string by a delimiter resulting in an unknown number of parts and how can I collect the results in an array?
In bash, for single character delimiters, you can use the split+glob operator (leaving expansions unquoted in list contexts) after having disabled the glob part: string='foo/bar baz/asd..' IFS=/ set -...
5votes
Array Declaration: Double Quotes & Parentheses
No, the former is not an array. For example, you can't use an index to select a member: arr=(a b c) echo "${arr[1]}" # b You can use an array to pass arguments, but you can't use a string. ...
4votes
Accepted
linux + how to convert variable to array
It seems that you have (maybe unintentionally) changed the important shell variable IFS in the script. Restoring it to its usual value or unsetting it (i.e. activating its default value) solves the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
bash-array × 80bash × 68
shell-script × 38
array × 23
linux × 5
shell × 5
for × 5
variable × 4
quoting × 3
gnu-parallel × 3
grep × 2
scripting × 2
git × 2
string × 2
variable-substitution × 2
whitespace × 2
associative-array × 2
indirection × 2
centos × 1
awk × 1
sed × 1
find × 1
zsh × 1
pipe × 1
virtualbox × 1