Another look at Arrays - BASH
Loading...
1,561
Loading...
Uploader Comments (metalx1000)
see all
All Comments (8)
-
Quoting error... examples in this video can fail if an argument contains whitespace.
~ $ a=('Kris' 'Tom' 'BOB Jenn'); for i in "${a[@]}"; do printf '%s\n' $i "$i"; done
Kris
Kris
Tom
Tom
BOB
Jenn
BOB Jenn
-
Thanks Sir you are best :)
Loading...
Now, I'm a bit confused on how it works. I've never seen the "For" command used before, so it's because of that, but I do understand that the For command can make the variable "i", but the in command kind of baffles me. For *Variable* in *Array numbers*, other than that I get it, but I feel as though I should try looking for your other tutorial for the other commands.
theif519 9 months ago
@theif519: the "for" command will do something for each item inputted. so, for example:
for i in *;do echo "$i is a file";done
in this case the "*" will list all files in the current directory and create a loop for each one.
you can also do this:
for i in 1 2 3 4 5;do echo "$i";done
which will output a loop for each number given the same as this:
for i in {1..5};do echo "$i";done
obviously if you wanted to count to 100 the shorter way would be {1..100}
Hope I explained this well.
metalx1000 9 months ago
thanks for the tutorials and keep them coming! but i had a hunch this would work and it does, to display all contents in an array echo ${names[*]}
ybnormal798 1 year ago
@ybnormal798: Good call. Thanks.
metalx1000 1 year ago
thanks for your tutorial, really appreciate it. I'm having difficulty to understand, when or in what circumstance we have to put brackets and when we have to put( parenthesis. The other day i was working on a script, and it didn't work until, i've put 2 embedded parenthesis. And i don't know why, it was that way.
So if you know about these an could explain , it would be great. Thanks.
qcjn 1 year ago
@qcjn: Sure, I'll try to do a video on that soon. But, I'll try to give some examples now. Brackets [ and ] are used in things like if statements and while loop. Example:
if [ $x = 1 ];then
they are also used when creating arrays.
humm, I just woke up so maybe it's because I'm tired, but I can't think of when I would use parenthesis ( and ). I'm sure they are used for something. I'll have to get back to you on that one.
metalx1000 1 year ago