Questions tagged [bash-array]
The bash-array tag has no summary.
80 questions
38
votes
15
answers
50k
views
Bash - reverse an array
Is there a simple way to reverse an array?
#!/bin/bash
array=(1 2 3 4 5 6 7)
echo "${array[@]}"
so I would get: 7 6 5 4 3 2 1
instead of: 1 2 3 4 5 6 7
23
votes
3
answers
10k
views
How to remove new line added by readarray when using a delimiter?
VAR=a,b,c,d
# VAR=$(echo $VAR|tr -d '\n')
echo "[$VAR]"
readarray -td, ARR<<< "$VAR"
declare -p ARR
Result:
[a,b,c,d]
declare -a ARR=([0]="a" [1]="b" [2]="c" [3]=$'d\n')
How can I tell ...
17
votes
4
answers
17k
views
Run a command using arguments that come from an array
Suppose I have a graphical program named app. Usage example: app -t 'first tab' -t 'second tab' opens two 'tabs' in that program.
The question is: how can I execute the command (i.e. app) from within ...
7
votes
2
answers
10k
views
Find array length in zsh script
Is there a way to find the length of the array *(files names) in zsh without using a for loop to increment some variable?
I naively tried echo ${#*[@]} but it didn't work. (bash syntax are welcome ...
7
votes
1
answer
413
views
Bash 4.4 local readonly array variable scoping: bug?
The following script fails when run with bash 4.4.20(1)
#!/bin/bash
bar() {
local args=("y")
}
foo() {
local -r args=("x")
bar
}
foo
with error line 3: args: readonly ...
5
votes
2
answers
14k
views
How to pass an array as function argument but with other extra parameters?
The following post solution works as expected:
How to pass an array as function argument?
Therefore - from his answer:
function copyFiles() {
arr=("$@")
for i in "${arr[@]}"...
5
votes
1
answer
532
views
How to pipe multiple results into a command?
I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):
EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id ...
4
votes
2
answers
1k
views
Bash's read builtin errors on a string-based timeout option specification but not an array-based one. Why?
In reading through the source to fff to learn more about Bash programming, I saw a timeout option passed to read as an array here:
read "${read_flags[@]}" -srn 1 && key "$REPLY&...
4
votes
1
answer
3k
views
Why is "${ARRAY[@]}" expanded into multiple words, when it's quoted?
I don't understand why "${ARRAY[@]}" gets expanded to multiple words, when it's quoted ("...")?
Take this example:
IFS=":" read -ra ARRAY <<< "foo:bar:baz"
for e in "${ARRAY[@]}"; do echo $...
4
votes
2
answers
2k
views
How to slice an indexed array to obtain all elements between the first and last index?
I have an array tokens which contains tokens=( first one two three last ). How do I obtain the values ( one two three ) if I do not know how many numbers are in the array? I want to access everything ...
3
votes
3
answers
4k
views
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?
I need to process some strings containing paths. How do I split such a string by / as delimiter resulting in an unknown number of path-parts and how do I, in the end, extract the resulting path-parts?
...
3
votes
4
answers
3k
views
How do I select an array to loop through from an array of arrays?
#!/usr/bin/bash
ARGENT=("Nous devons économiser de l'argent."
"Je dois économiser de l'argent.")
BIENETRE=("Comment vas-tu?" "Tout va bien ?")
aoarrs=("${...
3
votes
1
answer
2k
views
What is the difference between ${array[*]} and ${array[@]}? When use each one over the other? [duplicate]
With the following code:
#! /bin/bash
declare -a arr=("element1"
"element2" "element3&...
3
votes
3
answers
8k
views
Find second largest value in array
I have an array like this:
array=(1 2 7 6)
and would like to search for the second largest value, with the output being
secondGreatest=6
Is there any way to do this in bash?
3
votes
2
answers
2k
views
How to extract and delete contents of a zip archive simultaneously?
I want to download and extract a large zip archive (>180 GB) containing multiple small files of a single file format onto an SSD, but I don't have enough storage for both the zip archive and the ...