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 52753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:57:22+00:00 2026-05-10T16:57:22+00:00

Please explain to me why the very last echo statement is blank? I expect

  • 0

Please explain to me why the very last echo statement is blank? I expect that XCODE is incremented in the while loop to a value of 1:

#!/bin/bash OUTPUT='name1 ip ip status' # normally output of another command with multi line output  if [ -z '$OUTPUT' ] then         echo 'Status WARN: No messages from SMcli'         exit $STATE_WARNING else         echo '$OUTPUT'|while read NAME IP1 IP2 STATUS         do                 if [ '$STATUS' != 'Optimal' ]                 then                         echo 'CRIT: $NAME - $STATUS'                         echo $((++XCODE))                 else                         echo 'OK: $NAME - $STATUS'                 fi         done fi  echo $XCODE 

I’ve tried using the following statement instead of the ++XCODE method

XCODE=`expr $XCODE + 1` 

and it too won’t print outside of the while statement. I think I’m missing something about variable scope here, but the ol’ man page isn’t showing it to me.

  • 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. 2026-05-10T16:57:23+00:00Added an answer on May 10, 2026 at 4:57 pm

    Because you’re piping into the while loop, a sub-shell is created to run the while loop.

    Now this child process has its own copy of the environment and can’t pass any variables back to its parent (as in any unix process).

    Therefore you’ll need to restructure so that you’re not piping into the loop. Alternatively you could run in a function, for example, and echo the value you want returned from the sub-process.

    http://tldp.org/LDP/abs/html/subshells.html#SUBSHELL

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

Sidebar

Related Questions

Please explain invokeAndWait() method in SwingUtilities.I am unable to understand this. Explain it very
I am very new to CakePHP. Could you please explain me the steps needed
In WPF: Can someone please explain the relationship between DependencyProperty and Databinding? I have
Could you please explain what the practical usage is for the internal keyword in
Could someone please explain the best way to connect to an Interbase 7.1 database
Could somebody please explain to me what happens here? I am creating a binding
Can someone please explain me what's the difference between Swing and AWT? Are there
Can someone please explain why int (0.4 * 10.0) is 4 yet int ((2.4
Could someone please explain? I couldn't find anything on the internet, everything talks about
Could you please explain me, what is the different between API functions AllocConsole and

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.