0
results=
results['startlogdate']="Start time"
results['endlogdate']="$finish_time"
echo "${results[*]}"

I am trying to initialise the array and adding the value to array and echo the array. The code above is my attempt.

3
  • It's not clear what you're trying to do. What are you hoping that echo "${results[*]}" will print? Also -- is this Bash? Which version? Commented Jan 8, 2014 at 6:55
  • I am new to shell script, I want to add a element to array and echo that array Commented Jan 8, 2014 at 7:00
  • per your comments below, if you can't use bash can you use ksh? Most of the answers below will work with even the old version of ksh. change the top line of your script to #!/bin/ksh (or the correct path to ksh). Good luck. Commented Jan 8, 2014 at 12:03

3 Answers 3

1

In bash scripts, there are two kinds of arrays: numerically indexed and associatively indexed.

Depending on the version of your shell, associatively indexed arrays might not be supported.

Related to the example in your question, the correct syntax to obtain the values of the array, each as a separate word, is:

"${results[@]}"

To get the keys of the associative array, do:

"${!results[@]"

The script below demonstrates the use of an associative array. For more details, see the Arrays section in the bash manpage.

#!/bin/bash
# tst.sh
declare -A aa
aa[foo]=bar
aa[fee]=baz
aa[fie]=tar
for key in "${!aa[@]}" ; do
  printf "key: '%s'    val: '%s'\n" $key "${aa[$key]}"
done
echo "${aa[@]}"
exit

Here is the output:

$ bash tst.sh
key: 'foo'    val: 'bar'
key: 'fee'    val: 'baz'
key: 'fie'    val: 'tar'
tar bar baz

Finally, I've made available my library of array functions (aka "lists"), which I've been using for many years to make managing data in arrays easy.

Check out https://github.com/aks/bash-lib/blob/master/list-utils.sh

Even if you choose not to make use of the library, you can learn a lot about arrays by reading the code there.

Good luck.

Sign up to request clarification or add additional context in comments.

5 Comments

My script is not in bash ,sh shell script. declare not found error is coming
On some Linux/Unix systems, sh is the same as bash. But, if not, then associative arrays are not supported. In this case, simply use numerically indexed arrays. If you wish to use keywords, assign numeric values to the keywords, and they can still be used as indexes.
Even on systems that use sh by default, you can still use bash in a script, with the #!/bin/bash syntax on the first line of the script. If your system does not have /bin/bash, then you can download and install bash into /usr/local/bin/bash and invoke it with #!/usr/local/bin/bash in the first line of the script.
I have tried with above answer but I am getting following error:test_extract_tom_html.sh: 21: declare: not found test_extract_tom_html.sh: 22: aa[foo]=bar: not found test_extract_tom_html.sh: 23: aa[fee]=baz: not found test_extract_tom_html.sh: 24: aa[fie]=tar: not found test_extract_tom_html.sh: 27: Bad substitution
It's hard to debug your script within these comments. It would help if you would post your script as a gist (see gist.github.com) and share the link to the new gist. Also, please confirm which shells are available on your system, by showing the output of this command ls -l /bin/*sh
0

If want to use array in bash. you will be able to do in two ways.

  1. Declare an array in bash.
    declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');
    echo ${Unix[0]} # Prints the first element
    echo ${Unix[*]} # prints all the elements of an array

  2. Use directly (i.e) without declare. Unix[0]='Debian';Unix[1]='Red hat'

2 Comments

I am getting same error,One thing It is not bash script, normal shell script
This is an example of numerically indexed arrays. The question asked about associatively indexed arrays.
0

finish_time=`date`

results[0]="Start time"

results[1]="$finish_time"

echo ${results[@]}

Output: Start time Wed Jan 8 12:25:14 IST 2014

Number of elements: echo ${#results[@]}

Arrays in bash are zero indexed, so ${results[0]} will be "Start time" and ${results[1]} will be "Wed Jan 8 12:25:14 IST 2014"

2 Comments

I am getting following error :results[0]=Start time: not found test_extract_tom_html.sh: 74: results[1]=date: not found test_extract_tom_html.sh: 76: Bad substitution
This does not address the question about associatively indexed arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.