Questions tagged [bash-array]
86 questions
79
votes
5 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
- 8,247
38
votes
15 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
- 6,264
23
votes
3 answers
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 readarray not to add the final newline \n ? What is the meaning…
user1156544
- 359
17
votes
4 answers
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 a bash script if the number of arguments can…
Flux
- 3,378
- 5
- 29
- 60
9
votes
3 answers
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} cocacola.com airtel.com Now I'm trying to delete…
Rahul Patil
- 25,805
7
votes
1 answer
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' "${!ufs[@]}" > two > three > one unset ufs["one"] printf '> %s\n' "${!ufs[@]}" >…
Chris Davies
- 130,320
7
votes
1 answer
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 variable but succeeds when run with bash 4.2.46(2), which makes sense after…
Dennis
- 73
7
votes
2 answers
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 as well)
Cristiano
- 73
5
votes
2 answers
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[@]}"; do echo "$i" done } array=("one 1" "two 2" "three 3") copyFiles…
Manuel Jordan
- 2,246
5
votes
1 answer
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 ${SharedFileSystem} | jq…
Carmageddon
- 161
4
votes
2 answers
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 between first and last (exclusive). echo…
sriganesh
- 111
4
votes
1 answer
What is the difference between ${array[*]} and ${array[@]}? When use each one over the other?
With the following code: #! /bin/bash declare -a arr=("element1" "element2" "element3" "element4" ) echo "1" echo "${arr[@]}" echo "2" echo ${arr[*]} The output is: 1 element1 element2 element3 element4 2 element1 element2 element3 element4 So the…
Manuel Jordan
- 2,246
4
votes
2 answers
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" The value of read_flags is set like this : read_flags=(-t 0.05) (The…
qmacro
- 143
4
votes
1 answer
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 $e; done foo bar baz Any other variable that I expand in…
Shuzheng
- 5,129
- 6
- 57
- 104
3
votes
2 answers
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 extracted contents. I know that it would be possible…