Questions tagged [bash-array]
The bash-array tag has no summary.
83 questions
7
votes
1
answer
365
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' ...
-3
votes
3
answers
146
views
How to pass an array as an argument to a function
In the below shell script I am passing an integer and a string array to a print function which should print each parameter separately, but I'm not able to. I don't want make a reference of passed ...
2
votes
1
answer
445
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" &...
2
votes
3
answers
495
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 /...
0
votes
4
answers
268
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 ...
38
votes
15
answers
51k
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
0
votes
2
answers
84
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 ...
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 ...
2
votes
1
answer
329
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 ...
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&...
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=("${...
0
votes
3
answers
174
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....
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/...
4
votes
1
answer
4k
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 $...
1
vote
1
answer
410
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" ...
2
votes
1
answer
345
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
...
2
votes
5
answers
4k
views
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 ...
3
votes
2
answers
754
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 ...
17
votes
4
answers
18k
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 ...
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[@]}"...
7
votes
1
answer
430
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 ...
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
496
views
Sort file array list in bash by date and bypass argument limit
So, I have a file array list in a bash shell, and I want to sort all the files in the array by the date modified, starting with the oldest being the first one in the array. However, I don't want to ...
0
votes
3
answers
196
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
...
23
votes
3
answers
11k
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 ...
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&...
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 ...
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
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&...
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 "...
1
vote
1
answer
970
views
How to separate long string into a string array with IFS and read, or any other method
I have a long line that comes as output from a git command: a=$(git submodule foreach git status). It looks like this:
a = "Entering 'Dir1/Subdir' On branch master Your branch is up to date with '...
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?
...
-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=("...
9
votes
3
answers
26k
views
How to unset range of array in Bash
I'm trying to delete range of array element but it's fail..
My array
root@ubuntu:~/work# echo ${a[@]}
cocacola.com airtel.com pepsi.com
Print 0-1 array looks ok
root@ubuntu:~/work# echo ${a[@]::2}
...
0
votes
2
answers
499
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_$...
1
vote
2
answers
3k
views
gnu parallel with bash array
I trying to run command recon-all with GNU parallel freesurfer preproc i have a bash array of list of patients to run 8 patents simultaneously:
root@4d8896dfec6c:/tmp# echo ${ids[@]}
G001 G002 G003 ...
2
votes
1
answer
3k
views
Bash - mix/merge/combine two different arrays with same length
I have two different arrays with the same length:
s=(c d e f g a b c)
f=(1 2 3 1 2 3 4 5)
how can I mix/merge/combine this two arrays, so I would get this output:
c1 d2 e3 f1 g2 a3 b4 c5
0
votes
2
answers
145
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
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 ...
0
votes
3
answers
722
views
Why a bash array remains empty if I pipe the result of a for loop, used to populate it, into zenity --progress?
I have an array with some data:
array1=( AAA BBB CCC DDD )
I want to populate an array of results from calling a certain API with the data in array1 and at the same time I want to show the progress ...
2
votes
1
answer
953
views
Can't access elements of an array built from readarray
I'm trying to build a basic REPL in bash.
The script dynamically populates a list of files in a directory for the user to run.
File space:
|
|\ scripts/
|| script1.sh
|| script2.sh
|
\ shell/
| ...
0
votes
2
answers
194
views
grep results with spaces and special chars
I am trying to grab results from lshw and add them to a a bash array so I can create a new string. I am using
lshw -class disk |egrep -A 7 .'-d' |grep 'product' |cut -b 17-
the output looks like this
...
-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
1
answer
306
views
Matching a variable in order to pull value from an array
The script will echo the different values stored in an array based on user input. However, the output is not correct and the wrong value is echoed.
AA_P=/root/run_Scripts/AA_P_run.sh
AA_S=/root/...
0
votes
1
answer
756
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:
...
-3
votes
1
answer
135
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 ...
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 ...
-1
votes
1
answer
1k
views
Using shell arrays to read every N lines in parallel of a file
So, i already know how to successfully read every N lines in parallel, and run a command on each of those lines:
while read -r i && read -r a && read -r b && read -r c &&...
1
vote
2
answers
2k
views
Why isn't $ARRAY+=$var working for me?
I am trying to add an element to a bash array. I looked at this question and tried to follow its advice.
This is my code:
selected_projects=()
for project_num in ${project_numbers[@]}; do
...
0
votes
1
answer
525
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 ...