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

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want this to be generic enough to handle… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer Given the constraints, I'd pick TrimIfOptionSet or TrimIfNeeded. TryTrim feels… May 11, 2026 at 11:51 pm
  • Editorial Team
    Editorial Team added an answer Generally, it should throw an exception. The List<T> implementation of… May 11, 2026 at 11:51 pm

Related Questions

Can someone please explain what the & does in the following: class TEST {
I've been trying to get into F# on and off for a while but
I have a rails model class class Model < ActiveRecord::Base has_many :object_collection def add_object(object)
I'm refactoring some PHP code and discovered that certain nested combinations of if ()

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.