Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:15:26+00:00 2026-05-28T03:15:26+00:00

Possible Duplicate: Capturing multiple line output to a bash variable I have what is

  • 0

Possible Duplicate:
Capturing multiple line output to a bash variable

I have what is probably a basic scripting question, but I haven’t been able to find an answer anywhere that I’ve looked.

I have an awk script that processes a file, and spits out a list of disabled systems. When I call it manually from the command line, I get formatted output back:

$awk -f awkscript datafile

The following notifications are currently disabled:

host: bar
host: foo

I am writing a wrapper script to call from my crontab, which will run the awk script, determine if there is any output, and email me if there is. It looks like (simplified):

BODY=`awk -f awkscript datafile`
if [ -n "$BODY" ]
then
echo $BODY | mailx -s "Disabled notifications" me@mail.address
else
echo "Nothing is disabled"
fi

When run this way, and confirmed by adding an echo $BODY into the script, the output is stripped of the formatting (newlines are mainly what I’m concerned with), so I get output that looks like:

The following notitifications are currently disabled: host: bar host: foo

I’m trying to figure out how to preserve the formatting that is present if I run the command manually.

Things I’ve tried so far:

echo -e `cat datafile | awkscript` > /tmp/tmpfile
echo -e /tmp/tmpfile

I tried this because on my system (Solaris 5.10), using echo without the -e ignores standard escape sequences like \n . Didn’t work. I checked the tmpfile, and it doesn’t have any formatting in it, so the problem is happening when storing the output, not when printing it out.

BODY="$(awk -f awkscript datafile)"
echo -e "$BODY"

I tried this because everything I could find, including some other questions here on stackoverflow said that the problem was that the shell would replace whitespace codes with spaces if it wasn’t quoted. Didn’t work.

I’ve tried using printf instead of echo, using $(command) instead of `command`, and using a tempfile instead of a variable to store the output, but nothing seems to retain the formatting.

What am I missing, or is there another way to do this which avoids this problem all together?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:15:26+00:00Added an answer on May 28, 2026 at 3:15 am
    BODY=`awk -f awkscript datafile`
    if [ -n "$BODY" ]
    then echo "$BODY" | mailx -s "Disabled notifications" me@mail.address
    else echo "Nothing is disabled"
    fi
    

    Note the double quotes in the echo.

    You can simplify this version, too:

    echo -e `cat datafile | awkscript` > /tmp/tmpfile
    echo -e /tmp/tmpfile
    

    to just:

    tmpfile=/tmp/tmpfile.$$
    awkscript > $tmpfile
    if [ -s $tmpfile ]
    then mailx -s "Disabled notifications" me@mail.address < $tmpfile
    else echo "Nothing is disabled"
    fi
    

    Backquotes are useful (but better written as $(cmd args)) but do not have to be used everywhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: capturing echo into a variable I have a function that echos a
Possible Duplicate: Multiple forms on ASP.NET page i have a doubt. can we place
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Parsing text in C Say I have written to a text file
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: thread with multiple parameters How does one thread a sub with two
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: How to call a JavaScript function from PHP? I have a php
Possible Duplicate: How an uninitialised variable gets a garbage value? So when an undefined
Possible Duplicate: how to use foursquare API in android application? This is a question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.