Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [array]

A array is the simplest data-structure for storing items in continuously memory

0
votes
1answer
19 views

Different result for positional parameters expansion and custom array expansion

There is a difference in the output of commands that I don't understand, as stated below: INPUT="$@" echo "$INPUT ${#INPUT} ${#INPUT[@]} ${#@}" # outputs: a b c 5 1 3 arr=(a b c) echo "$arr ${#arr} $...
0
votes
2answers
32 views

bash: convert array into string preserving white spaces

I am just trying to convert an array into string, while preserving the white spaces. This is what I have: INPUT=$1 readarray -t arr < <(grep -o . <<< "$INPUT") echo "${arr[*]}" I ...
0
votes
1answer
18 views

fold: outputs only the first character

I am using fold to make an array of characters from a string. INPUT=$1 arr=($(fold -w1 <<< "$INPUT")) echo "$arr" echo "$arr[@]" I use command ./myProgram 1234 to execute the script. ...
3
votes
2answers
105 views

How to store (and load) a zsh String array to a file?

I want to store an array of some quotes (so basically real-world strings with new lines) in a file. How can I achieve it? I thought of setting the IFS to something like “xxxxxxxxx74765xxx” (which will ...
1
vote
3answers
52 views

bash array with space in element [on hold]

I have a text log file $ cat aaa 673 20160405 root "/path_to/gis/20160401/20160301_placement_map_org.dbf" "" 673 20160405 root "/path_to/gis/20160401/...
0
votes
1answer
30 views

grep for multiple elements of an array

I am trying to write a dd migration script that will do the following: read user input (max=4) into an array, called "array". the user will enter the logical volume names to be migrated. once each ...
1
vote
2answers
62 views

grep using array values and make it faster

array[1] is a string pulled from a 30k lines CSV: example: samsung black 2014 I need match those lines with one of the values contained within an array (arrayItems). arrayItems contains 221 values ...
0
votes
1answer
50 views

Check that 2 arrays of file names are the same

So I have the following: cat file | grep -E regex which gives a list of lines Now I also have a for loop the produces a list of files So it is like I have 2 arrays. But how do I check in bash if ...
0
votes
1answer
29 views

Sed search and delete matching two patterns one of which is a variable

I have a file that contains multiple instances of code that I need to delete. Here is an example: !/bin/bash mkdir /rootdir/pipeline_runs/oncology/analysis/sample_1_NA172_1 cd /rootdir/pipeline_runs/...
0
votes
0answers
22 views

Print all awk array elements [duplicate]

I want to print all the elements of a split string using awk (I have my reasons to use awk). Here's the gist of what I'm doing: thething="; " echo "What's the filename?" read filename echo " " ...
0
votes
1answer
17 views

Array inside an Array: Different syntax for Array in bash

I found the following example from here. But I can't understand how is array arr is being defined. a='domain.de;de;https' $ arr=(${a//;/ }) What is the advantage of defining it like this? Actually, ...
-2
votes
1answer
24 views

Is a delimiter included in the field in front of it?

If I am correct, in bash, readarray uses some delimiter to separate the input into fields, and the delimiter defaults to a newline. Unlike a separator, a delimiter is included in the field in ...
-2
votes
2answers
68 views

Why does double quoting an array expansion in here document not seem to work here?

I have two arrays $ arr1=( 1 2 2 3) $ arr2=( 2 3 3 4) Why does this generate strange output when I follow the good practice of double quoting an array expansion $ tsort << EOF > "${arr1[@]}...
9
votes
6answers
704 views

Bash sort array according to length of elements?

Given an array of strings, I would like to sort the array according to the length of each element. For example... array=( "tiny string" "the longest string in the list" "middle ...
0
votes
3answers
50 views

Duplicate elements of an array except the first and last elements

I have an array, and I would like to repeat each element except the first and last elements. For example if the array has five elements 1 2 3 4 5, then after repeating, its elements should be 1 2 2 3 ...
1
vote
1answer
94 views

Does readarray allow to specify line delimiter?

In bash read builtin has -d for us to specify line delimiter other than newline Does readarray provide some way to specify line delimiter? Is it correct that it has no an option for that purpose? ...
-2
votes
0answers
26 views

Where are the embedded newlines in the file are gone in the output of the following command? [duplicate]

I have a file: $ cat ffll abc 123 er3 I was wondering where the embedded newlines in the file are gone in the output of the following command, $ arr=( "$(cat ffll)" ) $ echo ${arr[1]} $ echo ${arr[...
0
votes
4answers
38 views

Array + why arrays are not in the Right place

I set an array named “disks” to the following words - sdb sdc sdd in bash shell: declare -A disks Machine=worker01 disks[$Machine]="sdb sdc sdd" now we print all words as the following: echo ${...
0
votes
1answer
34 views

Bash - How to put an array inside values of variable?

I have a web.txt file which contains: youtube facebook google My sh file contains: readarray web < web.txt url = www.website.com Commands I have used: readarray web < web.txt url = www."$...
0
votes
0answers
34 views

Problem with using gawk multi-dimension array in cygwin

Background: I'm trying to create a program that reads multiple files and creates a hist from the data in all of them. The data has 3 features, so I am building up a multi-dimensional array. the code ...
1
vote
2answers
56 views

How can I provide an array as a file argument to a script, so that each element is like a line in a file?

Suppose a bash script expects an argument to be a file. But we have a bash array, and would like to provide the array in some way as an argument to the script, as if there were a file whose lines are ...
1
vote
2answers
164 views

How to Use an array of strings in bash

I writte the following script load function should set the array disk[a]=1 and disk[b]=2 and so on Then the function out should print the array ${disk[a]} , and ${disk[b]} , and so on But what we ...
0
votes
0answers
14 views

Trouble with Array Appending [duplicate]

I'm iterating through a grep of my hosts file and am trying to separate the hostname and IP address into separate arrays. Now I tested my syntax: a=0 host=(a b c d) addr=(1 2 3 4) addr+=(1) echo ${...
0
votes
1answer
29 views

bash share array in “for do () & wait” loop

I have an issue with the script below. It uses a few sql statements in a for loop to set variables and produces a log file. The current db produces +- 1200 lines of output. The original version is ...
3
votes
2answers
61 views

assign output of command to array

When I run this command: cat output | grep -i state | sort | uniq | awk '{print $ 3}') the output is: 00x1 00x5 0080 To assign them to an array, I did this: STATUS_ARRAY=($(cat output | ...
17
votes
2answers
814 views

What type of operation is /#/- in “${my_array[@]/#/-}”?

The accepted answer to Transform an array into arguments of a command? uses the following Bash command: command "${my_array[@]/#/-}" "$1" I'm trying to figure out what the /#/- part does, exactly. ...
1
vote
2answers
203 views

tar “Cannot stat: No such file of directory” when passing array variables [closed]

I'm trying to write a bash script that will create an archive (tar) of files in a directory. The file extensions need to be passed in as arguments when calling the bash script (./backup.bash pdf txt ...
-1
votes
1answer
113 views

How to put whiptail parameters into an array / loop? [closed]

I have a small but embarrassing / annoying issue with coding whiptail command. I am passing "$x" parameters to function for whiptail to process. It works as expected , but looks ugly. I would like ...
3
votes
2answers
208 views

Bash scripting Array concepts

I have two separate lists. One contains 1000 hostnames and other contains 1000 associated IP's for that hosts. I need to generate config entries and push to my config file to add all hosts under our ...
0
votes
0answers
38 views

Why can't I mount array disks for dd?

When I have an USB disk recognized, I can do the 'dd' command and wipe data from the stick. I cannot do that with a SCSI disk from array such as NetApp. They don't seem to mount at all. The disk would ...
1
vote
2answers
57 views

to run a command for various variables in shell script

So, i'm storing the values of the first column returned from a sql query in a variable(var)..it's supposed to be an array type, because the sql query returns multiple rows, and later,these values ...
1
vote
1answer
33 views

How to delete existing elements from an array and keep appending new elements afterwards

Below is an example, where I am using two arrays which could be initiated with many elements, for now having "None" as the only element, is there any way to delete existing element/elements and then ...
1
vote
4answers
213 views

How to iterate through an array of numbers in shell-script?

I am trying to iterate through an array created after some command execution. The code used is: #!/bin/bash mailx -H|grep '^ [UN]'>ListOfMessages.txt msgNumbers=`cut -c 4-5 ListOfMessages.txt` ...
1
vote
1answer
53 views

Bash array using system positions limits? [closed]

Here is my array declaration arraySYSTEM=( "$@" ) It can have up to 10 "system position(?)" members. I'm using it to build text parameters passed to whiptail function. Then I select the parameter ...
0
votes
1answer
31 views

Find overlap/intersection of lists with bash

Say I have these two "lists": #!/usr/bin/env bash git fetch origin; first_list=( ); second_list=( ); git branch --merged "remotes/origin/dev" | tr -d ' *' | while read branch; do first_list+=(...
0
votes
1answer
48 views

command not found assigning value to array in bash

I'm trying to assign a value if the array at a certain index is empty, but I'm getting a command not found error. while IFS=$';' read -r -a array do # if empty set Not Available if [[ -z ${array[6]} ...
2
votes
2answers
1k views

jq - add objects from file into json array

I want to add an array with elements and value into an existing json file using jq. I already have a file (input.json) with { "id": 9, "version": 0, "lastUpdTs": 1532371267968, "name": "...
3
votes
1answer
180 views

Split zsh array from subshell by linebreak

I would like to instantiate a zsh array from a subshell myarray=($(somecommand) and I check if I received what I wanted with for element in $myarray ; do echo "===" ; echo $element ; echo "---" ; ...
0
votes
2answers
32 views

Bash : Give builded array to function as a list of argument?

I have this issue with borgbackup, but because the reaction is the same, I will use rsync in my example. I want to build an array of arguments by adding a prefix to each, and then give that array to ...
1
vote
7answers
123 views

Create PHP array from letters

I would like to create a PHP array from the Serbian alphabet. Out of а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш a b c č ć d dž đ e f g h i j k l lj m n nj o p r s š t u v z ž ...
2
votes
2answers
94 views

Index range of array doesn't allow you to iterate over a new line in bash, please help!

I'm working on a simple bash script that fixes a duplicate naming issue on a retropie. The script itself grabs any name that is mentioned more than once in a gameslist.xml file, then stores those in ...
0
votes
1answer
147 views

How to set empty array elements set to zero?

I'm writing a script that sends the result of commands to an output array. It involves checking servers for logs and retrieving their sizes, however there are cases in which the server has a failover ...
2
votes
1answer
62 views

Detect optional function argument (array)

Consider this function: function add_one(in_ar, each) { for (each in in_ar) { in_ar[each]++ } } I would like to modify it such that if a second array is provided, it would be used instead ...
0
votes
1answer
56 views

Generate a list of IPv6 into an array

I wrote this script a little while ago : #!/bin/bash #Default number of IPs NUM_IPS=48 if [ "$1" != "" ]; then NUM_IPS=$1 fi #Example of IPv6 supplied 2001:41d0:0303:6e1b::/64 read -p "...
2
votes
2answers
65 views

Why doesn't the separator works for array expansion?

I want to limit the change to the separator only to the following echo command not to the shell: $ myarr=(1 2 3) $ echo $( IFS="|"; echo "${myarr[@]}" ) 1 2 3 $ echo $( ( IFS="|"; echo "${myarr[@]}" )...
5
votes
3answers
3k views

How do I capture a MySQL result set in a bash array?

I'm using bash shell on CentOS 7. I want to run a MySQL query from a shell script and iterate over each row of results. If there were 4 rows returned, I thought I could capture the four rows in an ...
2
votes
2answers
72 views

Zsh, Indirect array variable assignment without using eval

I have a variable VARNAME which contains a name of another variable. I'd like to assign to this another variable without using eval. How can I do that? Th reason I don't want to use eval is the ...
6
votes
1answer
221 views

Unexpected outcome of a=“$@”

I'm struggling with this situation: $ set -- 1 2 3 $ a="$@" $ echo "$a" 1 2 3 What I find unexpected is the assignment itself. man bash says this about the "$@" expansion: When the expansion ...
2
votes
5answers
1k views

Number of elements/words in a shell array variable

I have looked at the question How to count number of words from String using shell on SO, which explains how to count words inside a variable. But this only counts one word inside my variable so I ...
2
votes
1answer
117 views

Why does this code work in bash v4.4 but not bash v3.2?

I have the following bash script: #!/bin/bash encl0=( 0,0 0,1 0,2 0,3 0,4 0,5 0,7 0,8 0,9 0,10 0,11 0,12 0,13 0,14 0,15 ) MISSING_DISKS=() OLDIFS=$IFS IFS=$'\n' MISSING_DISKS+=($({ printf '0 %s\n' {...