Posts Tagged ‘bash’

Here documents in bash

In my day to day work oftentimes I work with C, Bash scripting and Cross compiling for ARM and other interesting stuff. I feel programming is an art so as bash scripting. There are many things we come across after seeing the scripts written by highly experienced bash programmers. One of such concept was a here document.

In simple terms,

here documents can be treated as multiple line input provided to a command in a shell script.

Let us discuss here documents with simple examples:

Example 1:

wc -l << COUNT_LINES
Vasanth Raja

In the above example the start and end of the here document are specified with “COUNT_LINES“. Input to “wc -l” command is the three lines in between the COUNT_LINES. As expected the output to this is “3“.

Example 2:

Oftentimes I see the here documents used in conjunction with cat. This is because when we want to print set of lines to the terminal, this comes in handy. For example


The invoked script name: $0
This invoked script direcotry: `dirname $0`
This is a second line


Few important points are

1. In here documents we can substitute command outputs.

In the above example $0 and `dirname $0` are replaced with the command outputs.

2. All the text in the here document will be printed exactly to the standard output. That means leading tabs, spaces are preserved exactly.

Suppose if you wish to remove the leading tabs from the here document, the same can be achieved in the following manner

cat << – USAGE_HELP

The invoked script name: $0
This invoked script direcotry: `dirname $0`
This is a second line


Just use a ‘-‘ symbol just after the symbol “<<“.

In a nutshell, here documents comes very hand when you want to give multiple line input to a command. Moreover, here documents are simple to use and easy to understand.

vim backspace issue quick fix

vim is one of the extraordinary editors that I’ve ever used. One of the best thing I enjoy is the total customization it offers from fonts to key bindings, backgrounds and a lot more.

Recently I’ve came across backspace not working on vim editor even after the backspace is set in ~/.vimrc.

When I first started using vim I’ve encountered this issue and the quick thing which fixed is the following line.

set backspace=2

you can find more description in the vim tips here.

Even after adding this I’ve found my colleagues facing the same issue. Then the quick fix was to add the following line in your .vimrc

set backspace=indent,eol,start
indent  allow backspacing over autoindent
eol     allow backspacing over line breaks (join lines)
start   allow backspacing over the start of insert; CTRL-W and CTRL-U
        stop once at the start of insert.

The above line has proved proper solution for many. But I’ve seen cases where even after having those lines backspace prints “^?”.

This was rather annoying to many. one other quick fix for this is as follows

stty erase ^?

Run the above command on the shell and try appending the above two lines in your ~/.vimrc as follows

set backspace=2

set backspace=indent,eol,start

Enjoy editing in vim. If you find any other better fix for this issue please leave a comment below. Thanks for reading this post.