2022 Developer Survey is open! Take survey.

Questions tagged [bash-array]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
2 answers
30 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 ...
user avatar
  • 1
0 votes
2 answers
44 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_$...
user avatar
2 votes
5 answers
301 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 ...
user avatar
  • 1,631
-3 votes
1 answer
43 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 ...
user avatar
  • 5
0 votes
0 answers
27 views

How do I get this code to evaluate all arms of if-else? Why does Bash seem to interpret the characters that I input as integers? [duplicate]

The last elif arm does not get executed: #!/usr/bin/bash searches=("feet" "axilas" "lactant") length=${#searches[@]} searchFunction() { echo "Enter the ...
user avatar
1 vote
2 answers
355 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[@]}"...
user avatar
0 votes
1 answer
117 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&...
user avatar
2 votes
1 answer
27 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 "\...
user avatar
0 votes
0 answers
35 views

How to pass an array whose elements contain spaces from a script to a function in bash/zsh?

I'm using the zsh shell but would like solution that would also work in bash. I have a script and a function I want to use together. The script myfilter: #!/bin/bash result=() for arg in "$@&...
user avatar
0 votes
1 answer
44 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/...
user avatar
  • 27
-1 votes
1 answer
64 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=("...
user avatar
  • 2,517
0 votes
3 answers
76 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 ...
user avatar
  • 153
0 votes
2 answers
77 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 ...
user avatar
2 votes
0 answers
68 views

for loop and appending over list of arrays

I am new to Bash scripting and presently find myself dealing with a small problem in working with for loops, arrays and variable assignment/substitution which I do not know how to solve. Since I am ...
user avatar
1 vote
2 answers
143 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 ...
user avatar
  • 51
0 votes
0 answers
12 views

bash scripting problem creating array [duplicate]

I'm trying to make an array in a bash script. I keep running into a problem even though I feel like I've followed tutorials correctly. #!/bin/bash myArray=("cat" "dog" "mouse&...
user avatar
  • 1
4 votes
2 answers
397 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&...
user avatar
  • 143
1 vote
1 answer
402 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
user avatar
  • 4,750
0 votes
1 answer
33 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 ...
user avatar
  • 317
2 votes
1 answer
80 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/ | ...
user avatar
1 vote
2 answers
69 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[@]}"; ...
user avatar
2 votes
3 answers
827 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? ...
user avatar
  • 303
0 votes
1 answer
98 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: ...
user avatar
0 votes
2 answers
56 views

Concatenating string to form an existing variable name and working within array enclosure format

#!/bin/bash mat_1=(ServerAB ServerFR ServerPE ServerAM ServerHU) st="mat_1"; indirect_var='${'${st}'[@]}' #(Please, see the "--Desired Ouput Section--" in comments) #----- What ...
user avatar
  • 3
-1 votes
4 answers
225 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 ...
user avatar
  • 1
-1 votes
1 answer
265 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 &&...
user avatar
0 votes
2 answers
805 views

Shell script with a for loop and an “array”

How can I use this sh 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 "Creating ...
user avatar
0 votes
0 answers
39 views

Unexpected behavior during index assignment in a Bash array [duplicate]

I'm having trouble assigning values to a specific bash index, but apparently only when the index variable is set using a while read loop. Taking this code as a test example: #!/bin/bash read -d '' ...
user avatar
1 vote
0 answers
66 views

bash transfer multiple files in seq and uniq to sub folders

I have thousands or more than thousands of files like A20200727.2015+0200-2030+0200_SubNetwork=ppp,MeContext=xxx23,celltracefile_DUL1_3.bin.gz A20200727.2015+0200-2030+0200_SubNetwork=ppp,MeContext=...
user avatar
1 vote
2 answers
145 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 ...
user avatar
  • 113
0 votes
2 answers
232 views

bash scripting key value array in "etc/passwd" file

I want my /etc/passwd file to be as an array like this [user_id] => [home_directory] e.g. [0] => "/root" [1000] => "/home/user1" [1001] => "/home/user2&...
user avatar
  • 19
1 vote
2 answers
58 views

Why can't I convert a string variable into an array when some items include spaces?

For example, in the snippet below, (how) is it possible to make array2 identical to array1 while still using a str variable? ~$ { str='a "b c" d'; array1=(a "b c" d); array2=( $str ) echo "${array1[...
user avatar
  • 864
-1 votes
1 answer
109 views

percent per value from array, message if greater than 80%

I am trying to get an error message if one of the disks is filed more than 80%. So we have two arrays which have the data of disk partitions (they can have 3 partitions like in this example or more, ...
user avatar
0 votes
1 answer
201 views

Expanding a variable inside quotes

I am trying to get the files from S3 bucket,starting with certain prefix. To do so am using aws cli command in the bash script. Below is my code #!/bin/bash FILESIZE=$(mktemp) declare -a files=( "...
user avatar
  • 85
0 votes
1 answer
286 views

Pass Array Variable to MATLAB Function in Bash

Aim I am using multiple text files to set variables and array variables. These are the input arguments to a MATLAB function, that iteratively creates url download strings based on these variables, ...
user avatar
  • 3
0 votes
2 answers
123 views

checking if a rpm package exists in an array

I am trying to find out if certain RPM packages exists in an array. if any of the package is missing it will exit with the message "Package doesnt exist" .Here is my code #!/bin/bash echo "Checking ...
user avatar
  • 19
2 votes
1 answer
78 views

Bash array only executes first index

I am working with a server running Ubuntu 18.01 LTS and I'm trying to automate the backup of multiple virtual machines. I have the VM names in an array and then a for loop to shut down, backup and ...
user avatar
  • 51
0 votes
1 answer
87 views

Reference items in bash for loop from find command

Suppose I have this code: for i in $(find * -type f -name "*.txt"); do # echo [element by it's index] done How do I access, if possible, an element by it's index?
user avatar
1 vote
1 answer
55 views

issue with bash arrays containing both commands and paths

I'm trying to write a script to tailor my dotfile setup to each machine using git update-index -skip-worktree but it keeps chopping up the paths. I'm passing arrays starting with commands where every ...
user avatar
1 vote
1 answer
617 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 $...
user avatar
  • 3,307
1 vote
0 answers
141 views

Assigning array values while running a loop to a variable dynamically

The values present in indexArray are: 1 4 3 2 Below is the code snippet(This is not the complete code): while read -r line;do position=${indexArray[$counter]} # No value is assigned to "position" ...
user avatar
1 vote
1 answer
423 views

Array only returns one element

i'm trying to generate a script that ftp some files to a server using lftp. when i run these commands in shell: DBNAME=TESTDB ls -t /data*/${DBNAME,,}Backup/$DBNAME.0.db21.DBPART000.`date +%Y%m%d`...
user avatar
1 vote
2 answers
904 views

Merge duplicate keys in associative array BASH

I've got an array that contains duplicate items, e.g. THE_LIST=( "'item1' 'data1 data2'" "'item1' 'data2 data3'" "'item2' 'data4'" ) Based on the above, I want to create an associative array that ...
user avatar
  • 1,991
16 votes
3 answers
5k 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 ...
user avatar
5 votes
1 answer
290 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 ...
user avatar
1 vote
2 answers
268 views

Pass the name of an array in command line to reference the array in a function

I'm trying to learn more bash by updating my bash_profile so that I can quickly do some adb commands that I usually have to copy-paste. I found I was creating many similar functions that all looked ...
user avatar
1 vote
2 answers
2k 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 ...
user avatar
4 votes
2 answers
5k 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 ...
user avatar
3 votes
3 answers
6k 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?
user avatar
  • 202
1 vote
2 answers
6k views

Shell script-How to return maximum value in array?

I have a array: ARRAY=(12.5 6.2) I wish to return the maximum value in ARRAY which Output is 12.5 Anyone can share me ideas? I have try this: max=0 for v in ${ARRAY[@]}; do if (( $v > $max ...
user avatar