Questions tagged [bash-array]
71 questions
32
votes
14 answers
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
nath
- 5,430
- 9
- 45
- 87
22
votes
3 answers
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 readarray not to add the final newline \n? What is the meaning…
user1156544
- 349
- 2
- 9
7
votes
1 answer
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 variable but succeeds when run with bash 4.2.46(2), which makes sense…
Dennis
- 73
- 2
6
votes
2 answers
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 as well)
Cristiano
- 63
- 1
- 5
5
votes
2 answers
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[@]}";
do
echo "$i"
done
}
array=("one 1" "two 2"…
Manuel Jordan
- 1,414
- 9
- 33
5
votes
1 answer
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 ${SharedFileSystem} | jq…
Carmageddon
- 161
- 7
4
votes
2 answers
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 between first and last (exclusive).
echo…
sriganesh
- 101
- 1
- 6
4
votes
2 answers
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"
The value of read_flags is set like this:
read_flags=(-t 0.05)
(The…
qmacro
- 143
- 6
4
votes
1 answer
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 $e; done
foo
bar
baz
Any other variable that I expand in…
Shuzheng
- 4,023
- 1
- 31
- 71
3
votes
4 answers
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=("${ARGENT}" "${BIENETRE}")
select arr in "${aoarrs[@]}"; do
for el in "${arr[@]}"; do
…
John Smith
- 561
- 2
- 14
3
votes
2 answers
Iterating over array elements with gnu parallel
I have an input file, names.txt, with the 1 word per line:
apple
abble
aplle
With my bash script I am trying to achieve the following output:
apple and apple
apple and abble
apple and aplle
abble and apple
abble and abble
abble and…
duda13
- 33
- 2
3
votes
1 answer
What is the difference between ${array[*]} and ${array[@]}? When use each one over the other?
With the following code:
#! /bin/bash
declare -a arr=("element1"
"element2" "element3"
"element4" )
echo "1" …
Manuel Jordan
- 1,414
- 9
- 33
3
votes
3 answers
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?
k-a-v
- 202
- 2
- 9
2
votes
1 answer
What happens if I start a bash array with a big index?
I was trying to create a bash "multidimensional" array, I saw the ideas on using associative arrays, but I thought the simplest way to do it would be the following:
for i in 0 1 2
do
for j in 0 1 2
do
a[$i$j]="something"
…
2
votes
5 answers
echoing value in same indexes of 2 arrays simulataneously
I have 2 arrays to prcoess in bash script simultaneously.
First array contains sort of lables.
Second array contains values, as under
LABELS=(label1 label2 label3 labe4 )
VALUES=(91 18 7 4)
What's required is:
a loop that will echo the indexed-item…
Sollosa
- 1,887
- 4
- 19
- 32