this post was submitted on 05 Jan 2026
18 points (95.0% liked)

Linux

11067 readers
872 users here now

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS
 

Edit: SOLVED. Thank you all for your incredible insights! All of you helped me improve my code and knowledge! Special thanks to @Quibblekrust@thelemmy.club who just NAILED it. :)

I'm playing around with Bash just to learn.

LIST=$(ls); for i in $LIST; do echo "I found one!"; done

The variable "i" could literally be anything, as long as it doesn't have a special meaning for Bash, in which case I'd have to escape it, right? Anyway, my real question is: how does do (or rather the whole for-expression) know that "i" here means "for every line/item that ls outputs"? The above one liner works great and writes "I found one!" the number of times corresponding to the number of lines or items that ls outputs. But I would like to understand why it worked...

I'm a complete beginner at both Bash and C, but I understand some basic concepts.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] emotional_soup_88@programming.dev 1 points 1 week ago* (last edited 1 week ago) (1 children)

Reading this part of the Bash manual for the third time today, I think I finally understood it better, thanks to this part in particular:

[...]execute commands once for each word in the resultant list [...]

In other words, whatever follows in is half expected to result in a list of words (items), each for which command is then executed. Beyond that, I guess I'd have to simply look at the logic behind for-expressions.

Thanks!

[โ€“] frongt@lemmy.zip 1 points 1 week ago

Yeah it's really not complicated, and it's nearly plain English. For item in things, do action.