Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initiate array element to 0 in bash?

Tags:

linux

bash

shell

declare -a MY_ARRAY=()

Does the declaration of array in this way in bash will initiate all the array elements to 0?

If not, How to initiate array element to 0?

like image 870
MOHAMED Avatar asked May 10 '13 16:05

MOHAMED


People also ask

How do I get the nth element of an array in Bash?

Accessing array elements in bash The first element of an array starts at index 0 and so to access the nth element of array you use the n -1 index. For example, to print the value of the 2 nd element of your files array, you can use the following echo statement: echo $ {files }

How to use an array in Bash?

Bash Beginner Series #4: Using Arrays in Bash 1 Creating your first array in a bash script. Let’s say you want to create a bash script timestamp.sh that updates the timestamp of five different files. 2 Accessing array elements in bash. ... 3 Adding array elements in bash. ... 4 Deleting array elements in bash. ...

How to initialize an array by 0 in Java?

If an array already has elements and you want to initialize it by 0, you should use the fill () method of the Arrays class that fills the specified value to the array. See the example below:

Is there a shorthand syntax to initialize an array with 0?

But actually there is a shorthand syntax if you had a local array. If an array is partially initialized, elements that are not initialized receive the value 0 of the appropriate type. You could write: The compiler would fill the unwritten entries with zeros. Alternatively you could use memset to initialize the array at program startup:


2 Answers

Your example will declare/initialize an empty array.

If you want to initialize array members, you do something like this:

declare -a MY_ARRAY=(0 0 0 0) # this initializes an array with four members

If you want to initialize an array with 100 members, you can do this:

declare -a MY_ARRAY=( $(for i in {1..100}; do echo 0; done) )

Keep in mind that arrays in bash are not fixed length (nor do indices have to be consecutive). Therefore you can't initialize all members of the array unless you know what the number should be.

like image 190
Sir Athos Avatar answered Sep 28 '22 06:09

Sir Athos


Default Values with Associative Arrays

Bash arrays are not fixed-length arrays, so you can't pre-initialize all elements. Indexed arrays are also not sparse, so you can't really use default values the way you're thinking.

However, you can use associative arrays with an expansion for missing values. For example:

declare -A foo
echo "${foo[bar]:-baz}"

This will return "baz" for any missing key. As an alternative, rather than just returning a default value, you can actually set one for missing keys. For example:

echo "${foo[bar]:=baz}"

This alternate invocation will not just return "baz," but it will also store the value into the array for later use. Depending on your needs, either method should work for the use case you defined.

like image 40
Todd A. Jacobs Avatar answered Sep 28 '22 07:09

Todd A. Jacobs