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

VAR=`command`

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/theopenskyproject.com.mgmt =================================================================== — /var/named/chroot/var/named/internal/theopenskyproject.com.mgmt (revision 3056) +++ /var/named/chroot/var/named/internal/theopenskyproject.com.mgmt (working copy) @@ -33,3 +33,7 @@ $INCLUDE “internal/theopenskyproject.com.main” openn.lcl rev.192.168.100 shopopensky.com theopenskyproject.com theopenskyproject.com.main theopenskyproject.com.mgmt theopenskyproject.lcl theopenskyproject.qa theopenskyproject.stg IN CNAME theopenskyproject.com. + +;; 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/theopenskyproject.com.mgmt     (revision 3056)
+++ /var/named/chroot/var/named/internal/theopenskyproject.com.mgmt     (working copy)
@@ -33,3 +33,7 @@
$INCLUDE “internal/theopenskyproject.com.main”
*               IN CNAME    theopenskyproject.com.
+
+;; test
+;;
+;; test

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

echo “$DIFF” | mail -s “DNS Changes” group@domain.com



comments powered by Disqus