How to declare array in shell script?

2021-03-03T17:21:24

I am trying to declare an array in my shell script and then have written a for loop to access those array elements into another file. But it is only taking the first element. I've tried the below :

raw_cd=(1150 1151)
i=0


for i in "${raw_cd[@]}"
do
  grep -wr  "${raw_cd[@]}" file1.txt  > /opt/tmp/raw.txt
  echo "$i"
done

It is taking only the first element 1150 and also giving output as :-

file1.txt:|1150|
file1.txt:|1150|
file1.txt:|1150|

where am i doing wrong?

Copyright License:
Author:「Aviator」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/66454083/how-to-declare-array-in-shell-script

About “How to declare array in shell script?” questions

I am trying to declare an array in my shell script and then have written a for loop to access those array elements into another file. But it is only taking the first element. I've tried the below :
I'm trying to declare an empty array in Shell Script but I'm experiencing an error. #!/bin/bash list=$@ newlist=() for l in $list; do newlist+=($l) done echo "new&q
I want to put below declare in a shell script: proxy_set declare -x https_proxy="https://192.168.220.4:8080/" And then I execute it like below. $ ./proxy_set But "export" shows nothing happened...
For the life of me, I can't figure out how to declare and use new variables inside a shell block in a groovy script. For example, this shell block - sh """ export earlist='abc.ear,def.ear' ...
There are alot of guides, guides out there which show how to declare and define an array foo[0] = "abc" foo[1] = "def" What i am trying to achieve is to declare an array but not define it becaus...
I want to pass associate array as a single argument to shell script. mainshellscript.sh : #!/bin/bash declare -A cmdOptions=( [branch]=testing [directory]=someDir [function1key]=function1value [
I have a use case like below. declare filterAlias "if [[ $exeteral_variable == true ]]; then echo A; else echo B; fi" A = FOREACH loadData GENERATE a, b, c, d; B = FILTER loadData BY (a==‘xyz’); ...
Can you guys tell me how to write a shell script which declares for example a variable in the shell like this: var="foobar". It just has to write the exact same thing like I would write it manually...
For example: In Perl: @array = (1,2,3); system ("/tmp/a.sh @array" ); In my shell script, how do I handle this array in shell script? How do I handle the shell script to receive the argument...
I am trying to initialise an array in a shell script like below declare -a testArr=( 'A' 'B' ) Also like below testArr=( 'A' 'B' ) But in both the cases, I get the following error

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.