Line breaks in command output redirect

March 10, 2010    bash linux Sys Admin

Every now and then you need to save command output into a bash var from a command. Generally it looks like


That will put the stdout of command into $VAR

There is an issue if the output has linebreaks. Recently I had this issue trying to email SVN diffs for DNS changes. My command to get the diffs looked like this

DIFF=`svn diff /var/named/chroot/var/named/ /var/named/chroot/etc/`

If you run the following command

echo $DIFF

You get the following output

Index: /var/named/chroot/var/named/internal/ =================================================================== — /var/named/chroot/var/named/internal/ (revision 3056) +++ /var/named/chroot/var/named/internal/ (working copy) @@ -33,3 +33,7 @@ $INCLUDE “internal/” openn.lcl rev.192.168.100 theopenskyproject.lcl theopenskyproject.stg IN CNAME + +;; test +;; +;; test

You can see it is one big mess.. but if you add quotes around it

echo “$DIFF”

You get the following

— /var/named/chroot/var/named/internal/     (revision 3056)
+++ /var/named/chroot/var/named/internal/     (working copy)
@@ -33,3 +33,7 @@
$INCLUDE “internal/”
*               IN CNAME
+;; test
+;; test

As you can see that is much better. Now I can email it to the group

echo “$DIFF” | mail -s “DNS Changes”

comments powered by Disqus