Questions tagged [array]

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

625 questions
123
votes
5 answers

Is there a way of reading the last element of an array with bash?

If I have an array with 5 elements, for example: [a][b][c][d][e] Using echo ${myarray[4]} I can see what it holds. But what if I didn't know the number of elements in a given array? Is there a way of reading the last element of an unknown length…
3kstc
  • 4,616
  • 15
  • 33
  • 49
106
votes
2 answers

how to count the length of an array defined in bash?

I'm new to bash and can't find a good tutorial to answer my question. array=( item1 item2 item3 ) for name in ${array[@]}; do echo current/total ... some other codes done I want to calculate the current and total value, as the expected…
AGamePlayer
  • 7,415
  • 16
  • 46
  • 55
105
votes
4 answers

How to add/remove an element to/from the array in bash?

I have an array containing some elements, but I want to push new items to the beginning of the array; How do I do that?
SecureTech
  • 1,379
  • 3
  • 13
  • 18
74
votes
4 answers

How can I remove an element from an array completely?

unset array[0] removes the element but still if I do echo ${array[0]} I get a null value moreover there are other ways of doing this but if an element of an array contains spaces like below array[0]='james young' array[1]='mary' array[2]='randy…
munish
  • 7,825
  • 24
  • 71
  • 97
72
votes
4 answers

Transform an array into arguments of a command?

I have an array of "options" of a command. my_array=(option1 option2 option3) I want to call this command in a bash script, using the values from array as options. So, command $(some magic here with my_array) "$1" becomes: command -option1 -option2…
64
votes
3 answers

`Syntax error: "(" unexpected` when creating an array

I have two (Debian) Linux servers. I am creating a shell script. On the first one I create an array thus: #!/bin/bash target_array=( "/home/user/direct/filename -p123 -r" ) That works fine. But when I run this on the other server I get: Syntax…
IGGt
  • 2,137
  • 8
  • 28
  • 43
43
votes
4 answers

Bash: slice of positional parameters

How can I get a slice of $@ in Bash without first having to copy all positional parameters to another array like this? argv=( "$@" ) echo "${argv[@]:2}";
n.r.
  • 2,173
  • 3
  • 18
  • 30
43
votes
2 answers

Is there a reason why the first element of a Zsh array is indexed by 1 instead of 0?

From my experience with modern programming and scripting languages, I believe most programmers are generally accustomed to referring to the first element of an array as index 0 (zero). I'm sure I've heard of languages other than zsh starting array…
deekin
  • 610
  • 1
  • 6
  • 15
43
votes
4 answers

Arrays in Unix Bourne Shell

I am trying to use arrays in Bourne shell (/bin/sh). I found that the way to initialize array elements is: arr=(1 2 3) But it is encountering an error: syntax error at line 8: `arr=' unexpected Now the post where I found this syntax says it is for…
SubhasisM
  • 533
  • 1
  • 4
  • 6
40
votes
1 answer

is there a way to list all 'indexes IDs' (keys) on a bash associative array variable?

I have this array: declare -A astr I add elements to it: astr[elemA]=123 astr[elemB]=199 But later on I need to know what are the indexes IDs (elemA and elemB) and list them. echo "${astr[@]}" #this only get me the values...
Aquarius Power
  • 4,099
  • 5
  • 38
  • 56
40
votes
3 answers

What is the difference between [@] and [*] when referencing bash array values?

This Bash guide says: If the index number is @ or *, all members of an array are referenced. When I do this: LIST=(1 2 3) for i in "${LIST[@]}"; do echo "example.$i" done it gives the desired result: example.1 example.2 example.3 But when I…
arjan
  • 687
  • 2
  • 8
  • 10
33
votes
6 answers

BASH associative array printing

Is there a way to print an entire array ([key]=value) without looping over all elements? Assume I have created an array with some elements: declare -A array array=([a1]=1 [a2]=2 ... [b1]=bbb ... [f500]=abcdef) I can print back the entire array…
Dani_l
  • 4,720
  • 1
  • 18
  • 34
32
votes
14 answers

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
nath
  • 5,430
  • 9
  • 45
  • 87
31
votes
6 answers

parse one field from an JSON array into bash array

I have a JSON output that contains a list of objects stored in a variable. (I may not be phrasing that right) [ { "item1": "value1", "item2": "value2", "sub items": [ { "subitem": "subvalue" } ] }, { …
JpaytonWPD
  • 648
  • 1
  • 6
  • 16
25
votes
7 answers

How do I test if an item is in a bash array?

Help for a simple script #!/bin/bash array1=( prova1 prova2 slack64 ) a="slack64" b="ab" if [ $a = $b ] then echo "$a = $b : a is equal to b" else echo "$a = $b: a is not equal to b" fi This script simply doesn't work, I want a…
elbarna
  • 12,050
  • 22
  • 92
  • 170
1
2 3
41 42