LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-25-2010, 02:58 AM   #1
karak2007
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Rep: Reputation: 0
Smile What does the double less than sign (<<) means in creating a new file with cat?


Hi Guys,

Today I saw an article use the following cat command to create a new text file:

$ cat > first.sh << END

And then after press the 'return' buttion, a '>' sign appeared expecting you to input the content. If I type 'END' then the 'return' button, then a new text file is created with what I just typed...

I'd like to know what's the differnece in just type:

$ cat > first.sh

without the '<<' sign?

What '<<' really means? I cannot find it's meaning anywere... seems it's not in the redirection section...

Thanks!
 
Old 08-25-2010, 03:12 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

The input redirector << means use the current input stream as STDIN for the program until token is seen where token can be anything. Basically you'll get a prompt > where you can type in your commands or whatever until token (END, WHATEVER) is encountered on a new line.

Kind regards,

Eric

Last edited by EricTRA; 08-25-2010 at 03:13 AM. Reason: Added more info
 
1 members found this post helpful.
Old 08-25-2010, 05:49 AM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
It appears under the here document description :
Quote:
A limit string delineates (frames) the command list. The special symbol << designates the limit string....
Code:
#!/bin/bash

#  'echo' is fine for printing single line messages,
#+  but somewhat problematic for for message blocks.
#   A 'cat' here document overcomes this limitation.

cat <<End-of-message
-------------------------------------
This is line 1 of the message.
This is line 2 of the message.
This is line 3 of the message.
This is line 4 of the message.
This is the last line of the message.
-------------------------------------
End-of-message
 
Old 08-25-2010, 11:06 AM   #4
dv502
Member
 
Registered: Sep 2006
Location: USA - NYC
Distribution: Whatever icon you see!
Posts: 642

Rep: Reputation: 57
If you want to keep the contents of what you wrote, type

Quote:
cat << eof > file1
this is line 1
this is line 2
this is line 3
eof
When you type eof, the cat command will terminate and a file called file1 will be created.

Note: eof will not be in the file in case you are wondering.

Last edited by dv502; 08-25-2010 at 11:09 AM.
 
Old 08-25-2010, 09:21 PM   #5
karak2007
LQ Newbie
 
Registered: Aug 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks guys for the replies.
But strange things happens:
if using
$cat > any.sh << EOF

any variable is expanded (e.g., if I type $USER in, and later I cat any.sh, the var is expanded to its value).
But if I do not use '<<', just cat to a file and save it by ^D, variables are not expanded...
 
Old 08-25-2010, 10:52 PM   #6
scott_R
Member
 
Registered: Jul 2003
Location: Brighton, Michigan, USA
Distribution: Lots of distros in the past, now Linux Mint
Posts: 746

Rep: Reputation: 31
You need an extra level of single quotes if you want to prevent shell variable expansion in Bash, to pacify the '$'. I.e.:

~/testing $ cat > any.sh << EOF
hello
~/Downloads
$PS1

'$PS1'

'$'PS1''
^D

~/testing $ cat any.sh
hello
~/Downloads
\[\033[01;32m\]\w $ \[\033[00m\]

'\[\033[01;32m\]\w $ \[\033[00m\]'

'$'PS1''

Hope this helps!
 
Old 08-26-2010, 03:41 AM   #7
vsurlan
Member
 
Registered: Jun 2004
Location: Zagreb, Croatia
Distribution: Debian Lenny
Posts: 49

Rep: Reputation: 15
For a detailed explanation you can just read the BASH man page. Go here: Man BASH and look for 'Here Documents'.
 
  


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
cat FILE|sort +1 -n Returns ... +1: No such file or directory musician Linux - Newbie 1 07-23-2009 01:28 PM
cat file over ssh and put it into log file sinister1 Programming 2 04-24-2009 12:54 AM
filesize limit on 'cat $file | sed > $file' drkstr Linux - Software 2 07-10-2006 02:47 AM
trying to redirect text to a file to cat at later point. says file doesn't exist. dr_zayus69 Programming 1 10-02-2005 08:10 AM
What is a SIGN File? giddyupman Linux - Software 1 09-28-2004 09:16 AM


All times are GMT -5. The time now is 04:45 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration