This question already has answers here:
How can I join elements of an array in Bash?
(34 answers)
Closed 3 years ago.
How can I control how an array separates its elements? If you do:
echo ${array[*]}
it displays:
element1 element2 element3
and I would like it to be:
element1:element2:element3
Elements are joined by the first character of $IFS (internal field separator).
(IFS=':'; echo "${array[*]}")
Modifying $IFS has a lot of side effects. I recommend only changing it for a short duration inside a subshell so the rest of your script isn't affected.
Related
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 2 years ago.
I need to setup an array and use that to loop through and list the files. I started with this setup and it works.
file = (x1 x2 x3 x4 x5)
for var in ${file[#]}; do
echo $var
done
I have a requirement to add the timestamp on these variables, I tried a few ways and none of the seem to work. Here is one way I tried to define the array:
today=$(date +"%Y%m%d")
file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")
What would be the best way to define the array, where the array element has a variable?
The way you're using "$today" is correct, no problem there.
Don't put spaces around the equal sign. It needs to be file=(...) in both cases. Spaces are not allowed in assignments.
You can use Shell Check to catch these kinds of errors. Here's what it prints:
file = (x1_"$today" x2_"$today" x3_"$today" x4_"$today" x5_"$today")
^-- SC2283: Remove spaces around = to assign (or use [ ] to compare, or quote '=' if literal).
^-- SC1036: '(' is invalid here. Did you forget to escape it?
^-- SC1088: Parsing stopped here. Invalid use of parentheses?
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Bash arbitrary glob pattern (with spaces) in for loop
(2 answers)
Closed 2 years ago.
I'm trying to read a bash array file by file but for files with spaces in them, it comes out as two separate files. How would I solve this?
file names:
file A
fileB
file_C
the output Im getting is like this
File
A
FileB
File_C
What Im trying to do is to get File A on one line
I know I can read the array at once but for now we need to do it with a for loop since we're gonna be doing operations to it later in the loop.
for i in "${array[#]}"; do
echo "$i"
done
This question already has answers here:
Accessing bash command line args $# vs $*
(5 answers)
Closed 6 years ago.
x=('hello world' "HELLO")
Both ${#x[*]} and ${#x[#]} print the same output.
I understand the difference between $# and $* but I am interested to see the difference without command line arguments.
Always use # expansion unless you have reason to use *. # was added to work around a problem.
The two don't ALWAYS expand the same. The troubles involving* start with spaces and other shell metacharacters (quotes in particular, but $ and more as well).
The * leaves the metacharacters open for the shell to process them again, which is usually bad if you went out of your way to get them into the array. The # protects them by expanding each array element as if it was a separately quoted value, leaving all metacharacters intact.
This question already has answers here:
Multi-dimensional arrays in Bash
(13 answers)
Closed 9 years ago.
i have a script which get datas from a file, but some parameters have more values than just one.
How can i make that my bash script can read as example:
user[3][1]=test1
user[3][2]=test2
Actually i make:
for i in $(seq ${#lala[#]}); do
${user[$i]}
done
which works. I have try it with ${user[$i][$i]} and seq ${#lala[#][#]}) but i cant get it running.
The script must then work with just [$i] and [$i][$i].
Can please somebody help me out, thanks a lot!
Well you're using wrong language/script, since BASH doesn't support multi-dimensional arrays. Having said that you can store a delimited text in each element of array that you can further break into array inside a loop.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
tcl array question - key with quotes
I have the following code:
set my_list1 {"a" "b"}
set my_list2 {"#1" "#2"}
array set my_array {}
foreach li1 $my_list1 li2 $my_list2 {
set my_array($li1) $li2
}
puts $my_array("a")
On the list line I get ERROR "can't read my_array("a"): no such element in array"
Why?
I have it, because when I write
set newVar "a"
puts $my_array($newVar)
it returns the value!
This is just one of those things in Tcl. The array element is not my_array("a") -- it's my_array(a). Don't include the quotes when referencing the array. They're actually not necessary, although in that case note harmful, when you're installing the data into the array in the first place -- i.e.,
set my_list1 {a b}
would be just fine.
Tcl looks enough like a "normal" programming language that it's easy to forget how primitive its parser really is. Remember that everything is broken down into "words" by whitespace. If a double-quote character isn't preceded by whitespace, it isn't at the start of a word, and it no longer has any special significance. A reference to an array element is a single word, and after variable interpolation, it has to have exactly the right text. You can't put quote marks around the element name because simply those quote marks are not part of the correct text of that word.