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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:59:01+00:00 2026-06-07T00:59:01+00:00

I want to do a very simple script: just want to find the newest

  • 0

I want to do a very simple script: just want to find the newest version of a program, say svn, on my computer. I want to load the result into a variable, say mysvn

So I make this script:


#!/bin/sh

mysvn="foobar"
best_ver=0
which -a svn | while read p
do
    version=$("$p" --version | grep 'version ' | grep -oE '[0-9.]+' | head -1)
    if [[ "$version" > "$best_ver" ]]
    then
        best_ver=$version
        mysvn="$p"
    fi
    echo $mysvn
done

echo $mysvn

Very simple in fact … but it does not work under rxvt (my pseudo-linux terminal), version 2.7.10, running under XP: the final output string is foobar.

Does anybody know why I have this problem?

I have been writing some scripts for the past few months, it is the first time I encounter such a behaviour.

Note: I know how to make it work, with a few changes (just put the main lines into $() )

  • 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-06-07T00:59:03+00:00Added an answer on June 7, 2026 at 12:59 am

    The reason this occurs is that the while loop is part of a pipeline, and (at least in bash) any shell commands in a pipeline always get executed in a subshell. When you set mysvn inside the loop, it gets set in the subshell; when the loop ends, the subshell exits and this value is lost. The parent shell’s value of mysvn never gets changed. See BashFAQ #024 and this previous question.

    The standard solution in bash is to use process substitution rather than a pipeline:

    while
    ...
    done < <(which -a svn)
    

    But note that this is a bash-only feature, and you must use #!/bin/bash as your shebang for this to work.

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

Sidebar

Related Questions

I want to write a very simple script , which takes a process name
I have a very simple jQuery script.. all I want it to do is
I just want to design this very simple website. Basically there are multiple pages
I have a very simple js script that works for hovers, but I want
I wrote a very simple perl script, and now I want to make it
Very simple Winforms application I want to deploy manually. Can all the referenced assemblies
I want to build a very simple GUI based application in linux, c++. Which
A very simple thing, and I can't get it to work. I want to
I'll be very simple: I want to be able to store Java objects from
Very simple question, but I want to start using a consistent naming convention for

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.