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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:23:59+00:00 2026-05-28T01:23:59+00:00

I have 2 different scripts doing basically the same: counting the subdirectories in the

  • 0

I have 2 different scripts doing basically the same: counting the subdirectories in the current directory:

c=0

ls -l | grep "^d" | while read zeile
do
    c=`expr $c + 1`
    echo $c
done

echo "Subdirs: $c"

and

c=0

while read zeile
do
    c=`expr $c + 1`
    echo $c
done < <(ls -l | grep "^d")

echo "Subdirs: $c"

My Problem is, that in the first version, “c” seems to lose it’s value after the while-loop has finished.

The outputs

1)

1
2
3
Subdirs: 0

2)

1
2
3
Subdirs: 3

Can anyone of you explain to me, why this is happening?

Thanks in advance
Alex

  • 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-28T01:24:00+00:00Added an answer on May 28, 2026 at 1:24 am

    In the first case, the assignment to c happens after the |, i.e. in a subshell. You cannot change a variable in a parent shell from a subshell.
    BTW, why do not you use let c++ instead of backquotes and expr?

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

Sidebar

Related Questions

I have used several different scripts that people have suggested for trying to parse
I am maintaining several Perl scripts that all have similar code blocks for different
I have different urls that points to the same code www.url1.com www.url2.com I need
Soon I'm going to have 3 identical scripts on 3 different VPS's and I
I have 3 different watir scripts testing different functionality of a website. I want
I have a directory named tinkering which has following subdirectories: saraswati\ and\ durga\ pooja
I have databases with different collations. I want to be able to script out
I have a bash script that simply calls different calls and redirect stdout and
Why cgi scripts have to stay in cgi-bin folder ? What is the difference
I have different php output in jQuery-based tabs. This output is formed from database

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.