Monday, 18 August 2014

Storing Multi-line commands in history using shopt

It is hard to reuse multi-line commands as the newlines are replaced by semicolon and the many line command is converted to a single line. For example:

for i in $( ls *.txt)
do
pwd
echo $i
cp $i /bin
done

After executing it, when you press up button to reuse it, the command becomes:

for i in $( ls *.txt); do pwd; echo $i; cp $i /bin; done

Forget about reusing, I cant even understand what it does when so many things are stuffed in one line!
I found a way using which it is possible to retain the newlines in history using shopt.

shopt -s lithist

Now when you press up button you will see the command in multiple lines! win!

If you want this to be enabled by default add "shopt -s lithist" at the end of file ~/.bashrc


UPDATE
Also multi-line commands are screwed up when you add comments. For example

for i in $(ls)
do
echo "a"
#echo $i
done

becomes for i in $(ls); do echo "a" done (note that there is no semicolon after echo "a". Hence it takes echo "a" done as one command). Hence you will end up scratching your head to figure out what went wrong :P




No comments:

Post a Comment