3

Files along with their contents:-

~$cat a
aaa
aaa
aaa
~$cat b
bbb
bbb
bbb
~$cat c
ccc
ccc
ccc

Commands that I need explanation:-

~$cat 0< a
aaa
aaa
aaa
~$cat 0< a b
bbb
bbb
bbb
~$cat 0< a b c
bbb
bbb
bbb
ccc
ccc
ccc

I need an explanation as to why contents of file 'a' were printed when I executed ~$cat 0< a and why the contents of file 'a' were not printed when I executed ~$cat 0< a b or ~$cat 0< a b c.

  • 1
    (1) I presume that you realize that 0< is equivalent to just plain <, and so nobody ever says 0<.  (2) Nobody else seems to have explained the real answer to your question (or, at least, the entire answer), namely, your commands are equivalent to cat < a, cat b  < a, and cat b c  < a, because I/O redirection is not an argument, and is not visible directly to the command, and can be placed anywhere in the command line.  So, in the first case, cat is being run with no arguments, while in the other cases it is being run with filename argument(s). – G-Man Nov 24 '15 at 22:12
4

cat reads stdin if run without arguments. If you give it arguments, it will read the arguments and not stdin. If you want it to read both its argument files and stdin, make /dev/stdin one of the arguments (according to where you want it to go in the concatenation):

cat 0<a /dev/stdin b c 

Notes:

  • - or /dev/fd/0 also works
  • 0< can simply be <
  • 1
    - is better since it doesn't rely on the explicit existence of those special device files. (Usually those are there, but during system startup or installation, or if /proc isn't mounted, they might not be.) So, to the OP: cat - b c <a will print out file a then b then c. You could mix it up: cat a - c < b which will print out the files in the same order as before. – Otheus Nov 24 '15 at 20:42
  • 1
    @Otheus I prefer the /dev devices. It's cleaner. No special treatment for a magic argument of - dependent on each individual application. Getting rid of the - = stdin convention is one of the reasons why those dev devices were created. – PSkocik Nov 24 '15 at 20:48
  • 2
    cat -, however, is defined by the POSIX standard. Neither /dev/stdin nor /dev/fd/0 is. – chepner Nov 24 '15 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.