Command understanding the output file destination in case of standard output!!!!!

I ran the following command.

cat abc.c > abc.c

I got message the following message from command cat:

cat: abc.c : input file is same as the output file

How the command came to know of the destination file name as the command is sending output to standard file.

Running stat on the filename, and fstat on standard output, would tell it that they were the same inode and hence the same file.

It was too late to warn you, however. The redirection >abc.c had already truncated the file to zero length, as it always does, before the shell even began to run cat.

This is very basic shell scripting 101 type stuff. I recommend picking up a book on shell scripting basics and starting from there.

FYI this thread has no RedHat specific content. There's a forum for shell scripting questions; that would be a more appropriate place to ask.

In UNIX there is always a way:

$ echo >lf
$ cat lf >>lf
cat: Cannot use lf as both input and output.
$ cat lf | cat >> lf
$ wc lf
2 0 2 lf
$

Now if the file was longer, it would expand until space on the device was exhausted or it hit a file size limit. So be good. The SAs know where to find you! :smiley: