Questions tagged [bash-array]
The bash-array tag has no summary.
83 questions
7
votes
1
answer
363
views
Cannot delete bash associative array element
Consider an associative array in bash (versions 5.2.15(1)-release and 5.2.21(1)-release):
declare -A ufs=(); ufs["one"]=1; ufs["two"]=2; ufs["three"]=3
printf '> %s\n' ...
2
votes
1
answer
431
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
4
answers
263
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
1
answer
326
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 ...
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 ...
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=("${...
1
vote
1
answer
1k
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&...
1
vote
3
answers
4k
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/...
7
votes
1
answer
428
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 ...
1
vote
1
answer
346
views
Completed stumped by trying to use a decimal value in an array for Bash
Here is some simple test code.
#!bin/bash
cpm=(0 1 0.094)
lv=1
attack=5
defense=9
stamina=16
echo $((cpm[lv]))
mycpm=$((cpm[lv]))
#mycpm=`echo "0.094" | bc -l`
cq=`echo "$attack*$...
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 ...
0
votes
2
answers
143
views
Bash - How to make a function "delete only non-matching array from user inputs"
Suppose, I have 1 file in a folder named B.py
Using this script, I make 3 files within that folder. The files are A.py B.py C.py.
read -r -p "Enter the filenames: " -a arr
for filenames in &...
0
votes
2
answers
498
views
Bash fails to work with dynamic array name (but it understands it)
I'm doing a script in bash where I need to declare arrays inside loops, so I made it like this:
variable=suffix
declare -a prefix_$variable='(item1 item2)'
then I should be able to use "prefix_$...
-3
votes
1
answer
133
views
Difference between two array printing methods in bash [duplicate]
I have an array declared in my script.
NAME[0]=Deepak
NAME[1]=Renuka
NAME[2]=Joe
NAME[3]=Alex
NAME[4]=Amir
echo "All Index: ${NAME[*]}"
echo "All Index: ${NAME[@]}"
There are two ...
5
votes
2
answers
15k
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[@]}"...
2
votes
1
answer
75
views
Why isn't the `else` arm executed in this Bash script (for loop through an array)?
The following code is meant to look for subdirectories in ~/Downloads. I run it with . ./script.sh. It will find them even when the user submits an incomplete name.
#!/usr/bin/bash
echo -e "\...
-1
votes
1
answer
1k
views
How to work with arrays of space separated strings in shell script?
I with to monitor if my access points pingable and store results into 0-1 string
I wrote a script but it works wrong
#/bin/bash
access_points=("tplink2" "redmi1")
#results=("...
0
votes
2
answers
1k
views
For-loop - appending to arrays with iterator in the array name
I have the following problem. I have an array arr with some values. I want to sort each value into a set of different - and already declared - arrays earr$j, i.e. arr[0] into earr1, arr[1] into earr2 ...
1
vote
2
answers
2k
views
Array Declaration: Double Quotes & Parentheses
Perhaps this is a stupid question but two hours on Google hasn't turned up anything on point.
Simply, does a difference exist in Bash between:
X="
a
b
c
"
and
X=(
a
b
c
)
The former ...
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&...
0
votes
1
answer
524
views
Adding options using bash arrays
I am using a bash script to call rsync commands. Have decided to collect some options in an array called oser. The idea is to look at what's different in
the two invocations and put that into the ...
1
vote
2
answers
192
views
Getting an array into a parallel bash script
I'm having a bit of difficulty understanding parallel procedures. Atm I'm trying to mass wipe hard drives, so have created a script, however it won't run in parallel.
for i in "${!wipe[@]}"; ...
0
votes
1
answer
753
views
How to both get the original and reverse order of an array?
So, Let's say i have an array arr, with two element in it:
read -a arr <<< "$@"
where i would then either use it in a function or script and input two string or element like so:
...
-1
votes
4
answers
2k
views
The sum of elements in an array
I'm new to bash script learning and I'm quiet confused how to do this code.
array1=(23 34 23 12 11 32 12 12 12 21)
array2=(12 13 14 43 42 23 32 11 10 22)
These are the two arrays, and I need to get ...
0
votes
2
answers
2k
views
Shell script with a for loop and an “array”
How can I use this shell script with for loop and an array.
I would like to call create condition for quality gate creation of sonarqube with a for loop. Example:
#!/bin/bash --login
echo "...