HERE
by Chris on Dec.22, 2008, under Tech
Holy wow I’ve not visited this blog in quite a while. Alright, I’ve had some time to settle in to my new digs at work and have been learning quite a lot (too much almost, I can’t keep all this in my brain.) So I figured I’d start to put some fun stuff out here that I’ve been learning. Maybe you’ll find some of it useful, maybe not. Whatever.
Let’s take a look at what is commonly known as a “HERE Tag” in Bash scripting/command line.
In Bash, you can use a “double-less than” combination to tell the shell to redirect all typed text beyond the tag to a command.
wc -l << HERE line one line two line three HERE
The above sequence would output the result of “3″. Here’s what’s going on.
The first part of the sequence is just the *nix “wc” command, which is used to count stuff. We use a -l switch to tell it to count lines in this case. Right after that is the << redirect and the tag. In the example I used “HERE” which is traditionally what most people will use, but you can use whatever you like as a tag name (and it doesn’t have to be uppercase either, but to keep things simple you might want to leave it upper).
Beyond the HERE tag, you enter in your lines to have redirected to the command, one at a time on a separate line each. Once you reach the end of your typed commands and you want to close out the HERE tag sequence, you enter in another “HERE” on a line by itself. It must not have any leading spaces, or anything following it to be considered the end of the tag.
Once the end is found, the lines are redirected to your command one line at a time, and in our case it just counts how many lines were sent to it…to give a result of 3.
You can try things out both at the command line, as well as inside a script.
