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
Setting Environment Variables in Linux Using Bash
How to View the List of Functions a Linux Shared Library Is Exporting
How to Specify the Library Version to Use At Link Time
Compiling C++ on Remote Linux Machine - "Clock Skew Detected" Warning
How to Find All Files Containing Specific Text on Linux
How to Get Full Path of a File
How to Write Data to Existing Process'S Stdin from External Process
"Unable to Find Remote Helper For 'Https'" During Git Clone
How to Configure Apache 2 to Run Perl Cgi Scripts
How to Insert a Text At the Beginning of a File
How to Get Unique Values from an Array in Bash
&&' Vs. '&' With the 'Test' Command in Bash
Maximum Length of Command Line Argument That Can Be Passed to Sql*Plus
How to Run a Script At a Certain Time on Linux