Feeds:
Posts
Comments

Archive for the ‘bash’ Category

let me first describe the problem that way annoying me for a long long long time.

i use tmux and do my compiling and coding in one window and then many a times i need to open a new window to do some other stuff. the problem i face is that the bash commands that i write in one window are not present in the new window’s bash history.

for example note a typical annoyance

cd code/genetic/convergence/zdt1/population100/

do some work and then create a new window in tmux (C-b c) in order to do some work in the same directory.

and now most probably you will be placed in your home folder if that is where you started tmux initially.  so now in the new window i have to do this long change directory command.  the long cd command will not be written to bash history unless the shell exits.

this was a major annoyance to me for a long time. i wanted all the bash sessions to share the history concurrently.  i found a hack which lets me do what i want.

put the following in your .bashrc

export PROMPT_COMMAND="$PROMPT_COMMAND; history -a"
shopt -s histappend

If the histappend shell option is enabled the lines are appended to the history file, otherwise the history file is overwritten. to find out more about histappend just do a man bash and search for histappend.

the history command we refer to here is the bash built-in history command to know more about it do a

help history

yes that is help history **not** man history

if i have understood it correctly i guess the cleverness of the hack is in automatically doing a “history -a” after each command and what better way than to modify the PROMPT_COMMAND which defines how the prompt should be constructed each time. sweet 🙂

Read Full Post »

task at hand

you have two files :: file1.txt and file2.txt

you want to insert the contents of file1.txt into file2.txt starting at line number 2

contents of file1.txt before the process

because i m stupid

contents of file2.txt before the process

why do you want to do a phd ?

ok then, may god rest your spirit in peace.

contents of file2.txt after the process

why do you want to do a phd ?

because i m stupid

ok then, may god rest your spirit in peace.

this is how you do it

sed ‘2r file1.txt’ < file2.txt > tempFile.txt

mv tempFile.txt file2.txt

the r stands for the read FILENAME command and is a GNU extension i believe. so it may not be available on all platforms. to find out more try
info sed
and in particular see the section 3.6 Less Frequently-Used Commands

Read Full Post »