Questions tagged [bash-array]
The bash-array tag has no summary.
80 questions
2
votes
1
answer
196
views
How can I take a sub-array in bash of the first N elements of a string array with elements containing spaces?
This question is similar to this one but it differs from it:
Consider this array with string elements which may contain spaces:
a@W:$ arr=("eins" "zwei" "eins plus zwei" &...
0
votes
2
answers
45
views
pipe to uniq from a variable not showing the desired output
I have a pipeline using array jobs and need to change the number of inputs for some steps. I thought about testing uniq since the only part changing in my folders are the last four characters (the hap ...
0
votes
4
answers
172
views
bash - slice quoted substrings delimited by spaces into array
following example:
string=" 'f o o' 'f oo' 'fo o' "
array=($string)
echo "${array[0]}"
outputs:
'f
while the expected output is:
'f o o'
The only solution I came with is by ...
2
votes
3
answers
434
views
Replace prefix string from lines in a file, and put into a bash array
In the file groupAfiles.txt are the lines:
file14
file2
file4
file9
I need a way to convert them to remove file and add /dev/loop and put them all in one line with a space between them.
/dev/loop14 /...
2
votes
1
answer
258
views
Why does printing an array with @ using printf in bash only print the first element?
I have an array
snapshots=(1 2 3 4)
When I run
printf "${snapshots[*]}\n"
It prints as expected
1 2 3 4
But when I run
printf "${snapshots[@]}\n"
It just prints
1
without a ...
0
votes
3
answers
117
views
bash array multiplication using bc
I am trying to multiply array values with values derived from the multiplication of a loop index using bc.
#!/bin/bash
n=10.0
bw=(1e-3 2.5e-4 1.11e-4 6.25e-5 4.0e-5 2.78e-5 2.04e-5 1.56e-5 1.29e-5 1....
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 ...
1
vote
1
answer
333
views
Access values of associative array whose name is passed as argument inside bash function
I've some associative arrays in a bash script which I need to pass to a function in which I need to access the keys and values as well.
declare -A gkp=( \
["arm64"]="ARM-64-bit" ...
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=("${...
2
votes
1
answer
287
views
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
...
1
vote
1
answer
741
views
Creating and appending to an array, mapfile vs arr+=(input) same thing or am I missing something?
Is there a case where mapfile has benefits over arr+=(input)?
Simple examples
mapfile array name, arr:
mkdir {1,2,3}
mapfile -t arr < <(ls)
declare -p arr
output:
declare -a arr=([0])="1&...
0
votes
3
answers
164
views
Unable to append to array using parallel
I can't append to an array when I use parallel, no issues using a for loop.
Parallel example:
append() { arr+=("$1"); }
export -f append
parallel -j 0 append ::: {1..4}
declare -p arr
...
1
vote
3
answers
3k
views
Bash: converting a string with both spaces and quotes to an array
I have a function (not created by me) that outputs a series of strings inside of quotes:
command <args>
“Foo”
“FooBar”
“Foo Bar”
“FooBar/Foo Bar”
When I try to assign it to an array (Bash; BSD/...
3
votes
2
answers
612
views
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 ...
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 ...