LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > General
User Name
Password
General This forum is for non-technical general discussion which can include both Linux and non-Linux topics. Have fun!

Notices


Reply
  Search this Thread
Old 07-29-2022, 04:00 PM   #1
generic.newb_
LQ Newbie
 
Registered: Apr 2021
Posts: 21

Rep: Reputation: Disabled
Mindboggling ambiguity of the "for" loop in BASH


I can't understand, how in the world does for loop delimit it's items.

>a for loop can loop through a variable containing filenames
>a for loop "can" loop through variable containing lines of text from a file. (lines in varaible what?)

This:
Code:
for item in $var
do
  command $item
done
means that for each "item" in $var for loop will execute "command $item"
$var could be also replaced by something like `cat file` or output of any other command. The thing after "in" is actually like the real input of the "for" bash-included command.

But I don't understand and have never heared anyone say how are those items delimited.? Does for loop actually loop through lines of it's "input?" Does it loop through words separated by whitespaces?

Only after boring experimentation I have determined that items in a for loop are in fact only delimited by whitespaces, not by lines or anything else. And articles like this one https://linuxhint.com/bash_for_each_line/ or any other instance of suggesting that for loop can loop through lines in a file is a lie and is only true in a case where new lines are the only whitespaces.

This should be a common knowledge, but it isn't even for people who explain how a for loop works. Either that or they do stupid things like suggesting for loop is looping through "lines" or through "files" on purpose to confuse anyone who actually wants to understand how it works, and give them a headache when they try to finally loop through a list of files with spaces in names (I see now, this is one of the real reasons to not put spaces in filenames so you can use for loop on them). This is not even funny that I'm doing experiments and backwards engineering for loop to learn that. It's just sad and utterly retarded.

All it takes to properly explain for loop in bash is something like
The "for" loop:
Code:
for <item> in <list of items delimited by whitespaces>
do
    command $item
done
But noone does it. Either that or my ego just doesn't allow me to learn from anywhere else than my own experience. For me This is like noone who "knows" wants to actually teach you anything, everyone who knows wants to only tell you enough to be useful to him. This is like a medival priest speaking to peasants about god but keeping them illiterate so they can't read the bible themselves.

Last edited by generic.newb_; 07-29-2022 at 04:08 PM.
 
Old 07-29-2022, 04:41 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,022

Rep: Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685Reputation: 5685
A loop iterates items as defined by the IFS. The IFS is the Internal Field Separator or Input Field Separator and defaults to a <space><tab><newline>. Yes you can loop through items in a text file, string, array etc and can change the IFS by just assigning the variable i.e. IFS=','

Lots of examples can be found online...

https://mywiki.wooledge.org/IFS

Last edited by michaelk; 07-29-2022 at 05:06 PM.
 
Old 07-29-2022, 04:48 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,424
Blog Entries: 4

Rep: Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838Reputation: 3838
I haven't used "Bash scripting" in years. Simply put a "shebang" – something like this – as the first line: #!/usr/bin/env perl ...

... then write the rest of the command in Perl. Or any other "real" interpreted programming language of your choosing ...

If you take a peek at the source-code of any of the "command-line scripts" provided with your chosen distro, you will invariably find that something like this has been done. IMHO, the extremely-primitive scripting capabilities of bash were never intended for "serious work," and that's why they invented "shebang." As usual, a very elegant and "Unix-ish" solution.

(The only shell that I know of which tried to implement a "serious" built-in programming language was Dr. Korn's shell, ksh. But I hardly ever encounter this shell in the field.)

Last edited by sundialsvcs; 07-29-2022 at 04:57 PM.
 
Old 07-29-2022, 06:02 PM   #4
generic.newb_
LQ Newbie
 
Registered: Apr 2021
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
A loop iterates items as defined by the IFS. The IFS is the Internal Field Separator or Input Field Separator and defaults to a <space><tab><newline>. Yes you can loop through items in a text file, string, array etc and can change the IFS by just assigning the variable i.e. IFS=','

Lots of examples can be found online...

https://mywiki.wooledge.org/IFS
Thanks, good to finally know something like IFS exists.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Ambiguity in free disk space in /home partition vharishankar Linux - General 15 02-22-2013 03:58 PM
Mindboggling Segfaults and crashes willistg Linux - General 4 05-12-2005 09:40 AM
Partition Number Ambiguity farzan Linux - Newbie 3 08-08-2004 11:10 AM
xforms ambiguity with lyx corbis_demon Linux - Software 0 07-27-2004 12:03 AM
Ambiguity -- Need clarification nealkochhar Linux - Newbie 3 06-01-2004 06:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > General

All times are GMT -5. The time now is 09:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration