$(< file ) and $( cat file )


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $(< file ) and $( cat file )
# 8  
Old 02-03-2018
It does not work that way. $(<file) is a special case, which is a faster alternative to $(cat file). If you are using anything other than just a file, it becomes something else. $( ... ) is just command sustitution, so if we leave out that, it becomes:
Code:
< /dev/urandom tr -dc '[:alnum:],@#:!?+-' | head -c10

This is the same as tr -dc '[:alnum:],@#:!?+-' < /dev/urandom | head -c10
Code:
cat /dev/urandom -- tr -dc '[:alnum:],@#:!?+-' | head -c10

This is strange, it effectively just means cat /dev/urandom | head -c10
Code:
cat /dev/urandom | tr -dc '[:alnum:],@#:!?+-' | head -c10

This is the same as the first one with UUOC
Code:
< /dev/urandom | tr -dc '[:alnum:],@#:!?+-'  | head -c10

This is equivalent to :< /dev/urandom | tr -dc '[:alnum:],@#:!?+-' | head -c10, which is that same as :
: | tr -dc '[:alnum:],@#:!?+-' | head -c10
This User Gave Thanks to Scrutinizer For This Post:
# 9  
Old 02-03-2018
Quote:
Originally Posted by Scrutinizer
. . .
Code:
cat /dev/urandom -- tr -dc '[:alnum:],@#:!?+-' | head -c10

This is strange,
. . .
It is. In fact, tr -dc etc. are interpreted as input files
Code:
cat: tr: No such file or directory
cat: -dc: No such file or directory
cat: '[:alnum:],@#:!?+-': No such file or directory

and generate error messages which are suppressed in above case as /dev/urandom never ends and the pipe is chopped off by the head -c10 from the other side.
This User Gave Thanks to RudiC For This Post:
# 10  
Old 02-03-2018
Yeah, I agree with you regarding the UUOC one.
The test with -- was just bulk try just to see if it would get the -- as an end of options and see how it would handle the rest of the command line.

I ran it on an AIX machine which didn't return an error message, but a fooled output instead, with some strange control character (the kind of output that sometimes may mess up your PuTTY screen ...)

As Yoda and Scruti noticed, I think the confusing point was that, $(< /dev/urandom tr -dc '[:alnum:],@#:!?+-' | head -c10 )
is not interpreted as the special case $(<filename) but as the $( cmd )

This brings me to the question :
Does the special case $(<filename) support only and strictly 1 file ?

Last edited by ctsgnb; 02-03-2018 at 05:37 AM..
# 11  
Old 02-03-2018
Quote:
Originally Posted by ctsgnb
This brings me to the question :
Does the special case $(<filename) support only and strictly 1 file ?
Passing more than one file to the redirection wont be interpreted that way, the second file will just be understood as a command name.
Code:
$(< file1 file2)

Using more than one redirection lead to undefined behavior.
Code:
$(< file1 <file2)

- ksh ignores all redirections but the first one, i.e. output file1. It doesn't check file2 for readability/existence.

- bash silently ignores the whole command, i.e. output nothing, however, it returns an error if file1 or file2 isn't readable (or doesn't exists)
This User Gave Thanks to jlliagre For This Post:
# 12  
Old 02-07-2018
Obviously $( < filename ) copies the stream with a shell-internal, just like the external command $( cat < filename ) does.
For a concatenation of file1 and file2 you have to use the external command $( cat file1 file2 )
or a string concatenation like this:
Code:
s=$( < /etc/passwd )$( < /etc/group )
echo "$s"

# 13  
Old 02-07-2018
Quote:
Originally Posted by jlliagre
Code:
$(< file1 file2)

Using more than one redirection lead to undefined behavior.
Perhaps this is just an aside, but wouldn't redirecting from a named pipe work? That is, if in $(< file1) file1 would be a named pipe being filled by cat file1 file2 ... ?

And second, wouldn't $(< $(cat file1 file2) ) also work? Right now i am travelling with this damn work-laptop and have no U*X-system at hand, so i can only speculate instead of trying....

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ssh cat file output into a file on local computer

Hello, I'm on a remote computer by SSH. How can I get the output of "cat file" into a file on the local computer? I cannot use scp, because it's blocked. something like: ssh root@remote_maschine "cat /file" > /locale_machine/file :rolleyes: (2 Replies)
Discussion started by: borsti007
2 Replies

2. UNIX for Advanced & Expert Users

for i in `cat file` do

in bash: for i in `cat file` ; do echo $i done; how will i do this in perl ? (10 Replies)
Discussion started by: linuxgeek
10 Replies

3. UNIX for Advanced & Expert Users

cat can not open file

Hi All, I have stumbled upon very unique issue. In my script I am doing cat file and then greping and cutting so as to assign the value to variable. My file is, <mxc_tl_load_extractdata_prop.bsh> DB_USER=test_oper hostname=xxx FTP_USER=test1_operate MAIL_LIST=xxx@yyy.com... (1 Reply)
Discussion started by: paragd
1 Replies

4. Shell Programming and Scripting

Cat file

how to cat a file by ignoring first line and last line (1 Reply)
Discussion started by: thelakbe
1 Replies

5. Shell Programming and Scripting

split a line of a file and cat a file with another

Hi, I have two files one.txt laptop boy apple two.txt unix linux OS openS I want to split one.txt into one line each and concatenate it with the two.txt output files onea.txt laptop (4 Replies)
Discussion started by: avatar_007
4 Replies

6. Shell Programming and Scripting

Cat all yesterdays file into one file?

I am looking for a command to take files with a specific date and cat them all into big file. I know I can use commands to list all of the files from a certain date. But I want to do that and take those files and make on large files containing all of them. Any help would be great. This is being... (1 Reply)
Discussion started by: Jcheetwood
1 Replies

7. UNIX for Dummies Questions & Answers

How to cat file

I want to cat a file with only show the line contain '/bin/bash' but don't show the line contain 'load' (don't show if the line contain 'load' and '/bin/bash' together), how to type in the command? thk a lot! (2 Replies)
Discussion started by: zp523444
2 Replies

8. UNIX for Dummies Questions & Answers

Easiest way to cat out first 100 lines of a file into a different file?

Not sure how to do this exactly.. just want to take the first 100 lines of a file and cat it out into a second file. I know I can do a more on a file and > it into a different file, but how can I make it so only the first 100 lines get moved over? (1 Reply)
Discussion started by: LordJezo
1 Replies

9. Shell Programming and Scripting

cat file problem

Hi, I wnat to read a fiel line by line and store each line in a variabel, so I made a for loop: for i in `cat file` ; do #do sth. done; The problem is, that in the file, there are lines with only asterisks like this... (3 Replies)
Discussion started by: bensky
3 Replies

10. UNIX for Dummies Questions & Answers

cat and lp or pr printing of file

Can you use cat to send the first 25 lines of a file to the printer? I'm thinking I can pipe it with '|' but I'm not school to check printer output. With the 'nl' used, all lines are numbered on the print out, but how does one number only the blank lines? Thanks:) (1 Reply)
Discussion started by: bitwize
1 Replies
Login or Register to Ask a Question