Skip to main content
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 ...
Inian's user avatar
  • 13.1k
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 ...
Cyrus's user avatar
  • 12.7k
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 ...
jimmij's user avatar
  • 48.5k
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 ...
Chris Davies's user avatar
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 ...
ilkkachu's user avatar
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[@]}" ...
muru's user avatar
  • 76.3k
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 ...
choroba's user avatar
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 ...
AdminBee's user avatar
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 ...
Kusalananda's user avatar
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&...
larsks's user avatar
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 ...
Michael Homer's user avatar
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 ...
Stéphane Chazelas's user avatar
7votes

Bash - reverse an array

Ugly, unmaintainable, but one-liner: eval eval echo "'\"\${array['{$((${#array[@]}-1))..0}']}\"'"
user23013's user avatar
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 ...
Stephen Kitt's user avatar
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.
Fedor Dikarev's user avatar
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
Paul Hodges's user avatar
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 ...
Stéphane Chazelas's user avatar
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 ...
ilkkachu's user avatar
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 ...
pLumo's user avatar
  • 23.1k
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 ...
Kusalananda's user avatar
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.&...
glenn jackman's user avatar
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=(&...
terdon's user avatar
  • 250k
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 ...
Ole Tange's user avatar
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[...
jesse_b's user avatar
  • 40.4k
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 ...
Jeff Schaller's user avatar
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=, ...
Stéphane Chazelas's user avatar
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 -...
Stéphane Chazelas's user avatar
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. ...
choroba's user avatar
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 ...
Hauke Laging's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible

close