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

  • Home
  • SEARCH
  • 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 7644619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:44:03+00:00 2026-05-31T09:44:03+00:00

I have this shell script here and its doing what i want it do.

  • 0

I have this shell script here and its doing what i want it do. However, when I try to print out the variables at the end, the $file variables appended to either small, medium or large do not show in the std output. What is the shell doing here and what can I do to fix it?

#!/bin/sh

# sorts txt files into small, medium and large
# depending on number of lines
# small <= 10
# medium <= 100
# large > 100

small="Small Files:"
medium="Medium Files:"
large="Large Files:"

for txtFile in *.txt
do
    wc -l $txtFile
done |
while read lineCount file
do
    if [ $lineCount -lt 10 ]
    then
        small="$small $file"

    elif [ $lineCount -lt 100 ]
    then
        medium="$medium $file"
    else
        large="$large $file"
    fi
done
echo "$small"
echo "$medium"
echo "$large" 
  • 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-31T09:44:04+00:00Added an answer on May 31, 2026 at 9:44 am

    When you use a pipeline, the stages of the pipeline are run as separate processes and the variables that are assigned do not propagate back to the main process. You can do it like this instead:

    small="Small Files:"
    medium="Medium Files:"
    large="Large Files:"
    
    for txtFile in *.txt
    do
      wc -l $txtFile
    done | (
      while read lineCount file
      do
        if [ $lineCount -lt 10 ]
        then
          small="$small $file"
        elif [ $lineCount -lt 100 ]
        then
          medium="$medium $file"
        else
          large="$large $file"
        fi
      done
      echo "$small"
      echo "$medium"
      echo "$large" 
    )
    

    The parentheses cause the while loop and the echo statements to be grouped into a single process, so the variable values are preserved.

    You can see this effect in a simpler example:

    x=5;echo | x=6;echo $x
    

    will print 5, whereas

    x=5;echo | (x=6;echo $x)
    

    will print 6.

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

Sidebar

Related Questions

A really simple shell script question. I have a file with something like this:
I'm creating a Shell Script, and I have a file like this called expressions.txt
For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null
I have a requirement in a shell script. I get this location information from
I have the following code in a shell script. This only seems to work
I have a shell script script here as below: #!/bin/bash CPUSELECTION=1 386SX off \
This question is based on the thread . I have the shell function function
I have tried : c(module_name). : but this only works from the shell, and
I have this code in jQuery, that I want to reimplement with the prototype
I have a custom shell script that runs each time a user logs in

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.