while IFS= read -r -d $'\0' file ... explanation

"while ifs= read -r line"
bash ifs
while ifs='' read -r line || [[ -n "$line" ]]; do
bash read file line by line
bash while read
while ifs= read -r line meaning
while ifs= read not working
ifs= read options

I do not understand this line of shell script. Doesn't the while statement need a 'test' or [ ] or [[ ]] expression that will set $? to 1 or 0? How does

while IFS= read -r -d $'\0'; do ...; done

do that? Any help understanding the syntax here is greatly appreciated.

In Bash, varname=value command runs command with the environment variable varname set to value (and all other environment variables inherited normally). So IFS= read -r -d $'\0' runs the command read -r -d $'\0' with the environment variable IFS set to the empty string (meaning no field separators).

Since read returns success (i.e., sets $? to 0) whenever it successfully reads input and doesn't encounter end-of-file, the overall effect is to loop over a set of NUL-separated records (saved in the variable REPLY).

Doesn't the while statement need a 'test' or [ ] or [[ ]] expression that will set $? to 1 or 0?

test and [ ... ] and [[ ... ]] aren't actually expressions, but commands. In Bash, every command returns either success (setting $? to 0) or failure (setting $? to a non-zero value, often 1).

(By the way, as nosid notes in a comment above, -d $'\0' is equivalent to -d ''. Bash variables are internally represented as C-style/NUL-terminated strings, so you can't really include a NUL in a string; e.g., echo $'a\0b' just prints a.)

What is the scope of IFS, Will that only change the value of IFS for that specific while loop? No, actually it will change it only for the read part. Within the while loop's body, and after that,� Stack Exchange network consists of 177 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

I do not understand this line of shell script. Doesn't the while statement need a 'test' or [ ] or [[ ]] expression that will set $? to 1 or 0? How does [the statement] do that?

Everyone is answering about the -d and IFS= parameter, but no one answered your question about test and [[..]].

All standard Unix commands will return a zero value on success and a non-zero value on exit. The while and if statements use that value to determine whether or not to continue to loop:

Let's say you do the following:

$ touch foo.txt   #Create a "foo.txt" file
$ ls foo.txt
foo.txt
$ echo $?
0
$

Notice that the ls command returned a zero on exit. Now try this:

$ rm foo.txt
$ ls foo.txt
ls: foo.txt: No such file or directory
$ echo $?
1
$

If foo.txt doesn't exist, the ls command returns a 1 on exit.

The if and while commands operate on that.

Go into another terminal window and create a foo.txt file. Then return to the original window and try this:

$ while ls foo.txt > /dev/null
> do
> echo "The file foo.txt exists"
> sleep 2
> done
The file foo.txt exists
The file foo.txt exists
The file foo.txt exists
...

Every two seconds, the while will loop through checking for the existence of foo.txt and print out that statement. Now, go back to the second terminal window and delete foo.txt. If you go back to the first terminal window, you'll see that the while loop ended.

You can do the same with the if:

$ if ls foo.txt > /dev/null
> then
>     echo "foo.txt exists"
> else
>     echo "Whoops, it's gone!"
> fi

This if statement will print either foo.txt exists or Whoops, it's gone! depending whether or not a file called foo.txt exists in the directory.

Notice that if and while work upon the return exit value of the command. There's no need for a test at all.

So why the test, [...], and [[...]] syntax?

Let's say you want to find out whether $foo is equal to $bar. That's where test comes into play. Try this:

$ foo=42
$ bar=42
$ test "$foo" -eq "$bar"
$ echo $?
0
$ bar=0
$ test "$foo" -eq "$bar"
$ echo $?
1

All the test command does is return a zero on true tests and a non-zero value on false tests. This allows you to use the test command in if and while statements.

$ if test $foo -eq $bar
> then
>     echo "foo is equal to bar"
> else
>     echo "foo and bar are different"
> fi
foo and bar are different
$

So what's [...]?

$ ls -il /bin/test /bin/[
10958 -rwxr-xr-x  2 root  wheel  18576 May 28 22:27 /bin/[
10958 -rwxr-xr-x  2 root  wheel  18576 May 28 22:27 /bin/test

That first column is the inode. If two files have the same inode, they are hard linked and refer to the same file. Notice that they're both 18,576 bytes on my system.

The [ is just another way to do the test command itself. Both of these lines are the same:

if test $foo -eq $bar
if [ $foo -eq $bar ]

The [ was created to make shell scripts look a wee bit nicer. However, underneath, it's running a test command which will return either a zero or non-zero value that if and while can use.

By the way, in Kornshell and BASH, both [ and test are builtin to the shell. But, they do reference the same builtin command.

The [[...]] is a special version of the test that first appeared in Kornshell. It allows for the use of pattern matches and it's a bit faster since it doesn't spawn another process to run the test command.

Read line from a file into a variable, The IFS being set to null retains leading and trailing whitespace on the lines. The occasional purist will save a process by: while IFS="" read TX; do ## process 5. Default IFS vs Custom IFS: But there is a difference with the default IFS and the IFS value set by the user. In case of default IFS, the shell tries to separate taking into account sequence of spaces, sequence of tabs as one single separator, whereas it is not the case with user set separator. 5a. Variable with more spaces:

What is the 'IFS'?, IFS stands for Input Internal Field Separator - it's a character that separates fields. expansion and to split lines into words with the read builtin command. While it defaults to whitespace (space, tab, and newline), it may be� В `while IFS = read..`, почему IFS не имеет никакого эффекта? У меня может быть что-то совершенно неправильное, но мне кажется убедительным, что настройка IFS как одной из команд в списке pre-do / done

No. The test command and [[ ...]] expression are merely constructs that supply an exit status (the only thing while cares about) that reflects the truth of the conditional expression it contains.

For example, the exit status of [[ $foo = bar ]] is 0 (success) if $foo has the value bar, and a non-zero exist status (specifically 1) otherwise.

A while loop executes its body as long as the exit status of the command following the while keyword is 0. In this code, read has a 0 exit status as long as it can read something from its input.

Shell - Read a text file with two words and extract data, There are fields that contain spaces, so the IFS value will not work properly. So try this instead: Code: while IFS=, read -a F do printf "%s\n" "${F[@]# }" done� The mission for IFS Labs is to help us make better strategic product decisions, and to shorten the time from vision to realized solutions. Within IFS Labs we can try new ideas and innovations, including some of a “wilder” and more experimental nature. Much, though not all, of what IFS Labs does eventually results in new products and

Remote while IFS - UNIX and Linux Forums, #!/bin/ksh #HOST="$(/bin/hostname)" HOST="server1" KPARMS=$(/sbin/sysctl -a ) OUTFILE=/var/tmp/sysctl.txt ssh -B ${HOST} "while IFS= read ; do echo� I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second (7 Replies)

GNU Parallel (Bash Reference Manual), GNU Parallel can replace certain common idioms that operate on lines read from a file (in this case, filenames listed one per line):. while IFS= read -r x;� IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Relentless Nerd and Passionate Eater of Cheese and Biscuits. Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day.

bash: variable loses value at end of while read loop, See argument @ Bash FAQ entry #24: "I set variables in a loop. Why do they suddenly disappear after the loop terminates? Or, why can't I pipe data to read? IFS CE delivers all the ingredients of a traditional call center ACD, while providing a full outbound campaign solution. Outbound campaigns and call-backs can be interspersed with inbound calls to maximize contact center productivity. IFS CE provides a complete solution for executing simple and complex campaigns.

Comments
  • Using -d $'\0' is misleading. Typically, -d '' is used instead.
  • I think the OP is also confused by the syntax of the while statement, not realising that arbitrary statements can be used.
  • @hvd: You may be right. I've expanded my answer a bit to address that directly.
  • Thank you so much ruakh.
  • While ls is probably a good choice for demonstration purposes, using it in actual programs is usually a mistake.
  • @tripleee It was merely for demo purposes. ls is an easy to understand command, and well known. If you want to know if a file exists, use [ -f ... ]`.