Thread: Redirecting <<

    #1
  1. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535

    Redirecting <<


    I am having a hard time figuring out this type of redirection.

    Evidently, it means use the current input stream as STDIN for the program until token is seen.

    Okay, so I try $ cat << hello, it displays any text I type until I type "hello" which evidently exists the program. Well, what good is that, and when should it be used?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    There's lots of uses for << TOKEN. It basically says "the text I enter ends when you see "TOKEN" on a line by itself". Normally, *nix commands end when it sees a new-line character. With <<, you can pass a multi-line text string to the command.

    For example, if you were writing a shell script that prints out a multi-line help message, you could do something like this:
    Code:
    cat <<END_OF_HELP
    Super Foonly - A program to help you do something important.
    
    Copyright (c) 2012, Yoyodyne Inc.
    Signed Ty Coon, President of Vice.
    END_OF_HELP
    and it will print out:
    Code:
    Super Foonly - A program to help you do something important.
    
    Copyright (c) 2012, Yoyodyne Inc.
    Signed Ty Coon, President of Vice.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Sep 2006
    Posts
    2,042
    Rep Power
    535
    Thanks Scorpion,

    Super Foonly and Yoyodyne? Nice!

    Don't know about the usefulness your example, but it does allow me to better understand. You use it whenever you want to allow line breaks to be sent to the shell.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    Other common uses for the here document are for scripts that write other scripts, or for input to another program. Remember, if the <<WORD is unquoted the system parameter expands and other things within the here document. Many programs use hyphen argument to indicate the input comes from stdin. Related sources can group in the same file.
    Code:
    #! /bin/bash
    gawk -f - $1 <<-EOF
    	/long/ {
    	  program = "statements"
    	}
    	END {
    	  print program
    	}
    EOF
    # more commands
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo