Expanding a Bash Array Only Gives the First Element

Expanding a bash array only gives the first element

$files expands to the first element of the array.
Try echo $files, it will only print the first element of the array.
The for loop prints only one element for the same reason.

To expand to all elements of the array you need to write as ${files[@]}.

The correct way to iterate over elements of a Bash array:

for file in "${files[@]}"

Why is bash only appending first element to array

Actually your files+=( "$input" ) expression is adding elements to your array but you are not iterating it correctly.

Your last loop should be:

for f in "${files[@]}"; do
echo "element is: $f"
done

Test (thanks to @fedorqui)

$ a+=(1)
$ a+=("hello")
$ a+=(3)
$ for i in "${a[@]}"; do echo "$i"; done
1
hello
3

Issue with copying bash array and only first element being present

I think it's a typo mistake, here replace:

# BAD: prints "one," and not "one,two,three,"
MY_ARR=("${SAVE_MY_ARR}")

By

# BAD: prints "one," and not "one,two,three,"
MY_ARR=("${SAVE_MY_ARR[@]}")

Adding the [@] to SAVE_MY_ARR.

Problem with executing only first element into array

Possibly this ssh call eats your input:

sudo sshpass -p 'some.pass' ssh -o StrictHostKeyChecking=no user@$term_IP "sudo test -d /root/nv9"
^^^

Try adding -n.

Bash i can't print all elements of an array but only the last one

You first issue is to add elements to an array, you do something like array+=("$item_to_add")

So, you might have something like this:

tur=( A B C D )

for item in "${tur[@]}"; do
arr+=("$item")
done
for e in "${arr[@]}"; do
printf "%s\n" "$e";
done

BTW: It is best to avoid Bash variable names INCAPS since Bash uses these for internal use. The convention is to use lower case for user variable names.

BTW: The second loop is functionally the same as:

$ printf "%s\n" "${arr[@]}"

And both loops can be replaced with:

arr=("${tur[@]}")             # copy the array
printf "%s\n" "${arr[@]}" # print the new array

how to append one element to a GNU bash array variable and use that array variable as arguments to an ELF executable

Merely += adds a string to an existing string. You probably want bismon_hello_args+=("$f");; (notice also the quotes). To call the program, use ./bismon "${bismon_hello_args[@]}" & (notice the quotes, again).

The syntax to use an array variable is different than the syntax for simple scalars. This syntax was inherited from ksh, which in turn needed to find a way to introduce new behavior without sacrificing compatibility with existing Bourne shell scripts.

Without the array modifiers, Bash simply accesses the first element of the array. (This confuses beginners and experienced practitioners alike.)

Append elements to an array in bash

It did work, but you're only echoing the first element of the array. Use this instead:

echo "${args[@]}"

Bash's syntax for arrays is confusing. Use ${args[@]} to get all the elements of the array. Using ${args} is equivalent to ${args[0]}, which gets the first element (at index 0).

See ShellCheck: Expanding an array without an index only gives the first element.

Also btw you can simplify let i=i+1 to ((i++)), but it's even simpler to use a C-style for loop. And also you don't need to define args before adding to it.

So:

#!/bin/bash
for ((i=0; i<5; ++i)); do
args+=($i)
echo "${args[@]}"
done

Shell Script for loop stops after the first iteration (very simple code)

$LIST expands to the first element in array LIST, it's basically the same thing as ${LIST[0]}. You need to use ${LIST[@]} in double-quotes to get each element as a separate word, like:

#!/bin/bash

LIST=()
LIST+=('aaa')
LIST+=('bbb')
LIST+=('ccc')

for i in "${LIST[@]}"
do
echo '----------'"$i"'----------'
done

c.f. Bash Reference Manual § Arrays



Related Topics



Leave a reply



Submit