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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:11:24+00:00 2026-05-23T02:11:24+00:00

I have a simple bash script, which forms part of an in house web

  • 0

I have a simple bash script, which forms part of an in house web app that I’ve developed.

It’s purpose is to automate deletion of thumbnails of images when the original image has been deleted by the user.

The script logs some basic status info to a file /var/log/images.log

#!/bin/bash
cd $thumbpath
filecount=0
# Purge extraneous thumbs
find . -type f | while read file
do
  if [ ! -f "$imagepath/$file" ]
  then
    filecount=$[$filecount+1]
    rm -f "$file"
  fi
done
echo `date`: $filecount extraneous thumbs removed>>/var/log/images.log

Whilst the script correctly deletes thumbs, it doesn’t correctly output the number of thumbs that are being purged, it always shows 0.

For example, having just manually created some orphaned thumbnails, and then running my script, the manually generated orphaned thumbs are deleted, but the log shows:

Thu Jun 9 23:30:12 BST 2011: 0 extraneous thumbs removed

What am I doing wrong that is stopping $filecounter from showing a number other than zero, when files are being deleted.

I’ve created the following bash script to test this, and this works perfectly, outputting 0 then 1:

#!/bin/bash
count=0
echo $count
count=$[$count+1]
echo $count

Edit:

Thanks for the answers, but why does the following work

$ x=3
$ x=$[$x+1]
$ echo $x
4

…and also the second example works, yet it doesn’t work in the first script?

Second Edit:

This works

count=0
echo Initial Value $count
for i in `seq 1 5`
do
  count=$[$count+1]
  echo $count
done
echo Final Value $count


Initial Value 0
1
2
3
4
5
Final Value 5

as does replacing count=$[$count+1] with count=$((count+1)), but not in my initial script.

  • 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-23T02:11:25+00:00Added an answer on May 23, 2026 at 2:11 am

    You’re using the wrong operator. Try using $(( ... )) instead, e.g.:

    $ x=4
    $ y=$((x + 1))
    $ echo $y
    5
    $
    

    EDIT
    The other problem you’re bumping into is down to the pipe. Bumped into this one before (with ksh, but wouldn’t suprise me to find that other shells have the same problem). The pipe is forking another bash process, so when you do the increment, filcount is getting incremented in the subshell that’s been forked after the pipe. This value isn’t passed back to the calling shell as the subshell has it’s own independent environment (environment variables are inherited in called processes, but called process cannot modify the environment of the calling process).

    As an example, this demonstrates that filecount gets incremented okay:

    #!/bin/bash
    
    filecount=0
    ls /bin | while read x
    do
            filecount=$((filecount + 1))
            echo $filecount
    done
    echo $filecount
    

    …so you should see filecount increase in the loop, but the final filecount will be zero because this echo belongs to the main shell, but the forked subshell (which consists purely of the while loop).

    One way you can get the value back is like this…

    #!/bin/bash
    
    filecount=0
    filecount=`ls /bin | while read x
    do
            filecount=$((filecount + 1))
            echo $filecount
    done | tail -1`
    echo $filecount
    

    This will only work if you don’t care about any other stdout output in the loop as this throws it all away apart from the last line we output (the final value of filecount). This works because we’re using stdout and stdin to feed the data back to the parent shell.

    Depending on your viewpoint this is either a nasty hack or a nifty bit of shell jiggery-pokery. I’ll leave you to decide what you think it is 🙂

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

Sidebar

Related Questions

I have a simple test bash script which looks like that: #!/bin/bash cmd=rsync -rv
I have a bash script that tests whether the sftp connection exists, very simple
I have a simple bash script, called print_code.sh , that takes a file name,
I have written a very simple bash script to help me migrate from dev
I have a bash script that simply calls different calls and redirect stdout and
I am trying to write a simple bash script that will search for files
I have a bash script which I'm reading the results from in my program.
I have a Bash script that basically initializes an application and sets parameters. One
I have a bash script which takes a single argument and returns around 8-10
I have a simple python script which calls 6 threads and for some reason

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.