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

The Archive Base Latest Questions

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

I am new to Shell script. Just have a small question about some script

  • 0

I am new to Shell script. Just have a small question about some script I wrote.

pkgs="./cppcheck-1.48.tar.bz2 ./sshpass-1.04.tar.gz"
for pkg in $pkgs ; do
echo ${pkg} | grep -qE "bz2$"

if [ $? ] ; then
                echo "here1"
else
                echo "here2"            
fi

done

It always prints here1, i.e. if condition is always true. I am not able to pin point any reason for it till now. Please point me towards right direction.

I know I can use shell’s switch command. Just want to know what is wrong with this. Shell is /bin/bash

[EDIT] – switch statement works flawlessly. For those who face this problem some other time

case ${pkg} in
     *bz2) echo "here1";;
     *) echo "here2";;
esac
  • 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-23T09:08:04+00:00Added an answer on May 23, 2026 at 9:08 am

    if [ $? ] does not work as you might expect:

    $ false ; if [ $? ] ; then echo "true" ; else echo "false" ; fi 
    true
    $ true ; if [ $? ] ; then echo "true" ; else echo "false" ; fi 
    true
    

    It’s easy enough to fix by comparing against 0 explicitly:

    $ false ; if [ $? -eq 0 ] ; then echo "true" ; else echo "false" ; fi 
    false
    $ true ; if [ $? -eq 0 ] ; then echo "true" ; else echo "false" ; fi 
    true
    

    So for your script:

    pkgs="./cppcheck-1.48.tar.bz2 ./sshpass-1.04.tar.gz"
    for pkg in $pkgs ; do
    echo ${pkg} | grep -qE "bz2$"
    
    if [ $? -eq 0 ] ; then
                    echo "here1"
    else
                    echo "here2"            
    fi
    
    done
    
    here1
    here2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have shell scripting knowledge. I have written a small shell script which will
I have a tiny shell script that writes to a serial device: #!/usr/bin/ruby require
I have a bash shell script that loops through all child directories (but not
I have a shell script that starts tomcat using 'catalina.sh start'. This script runs
I'm fairly new at programming, but I've wondered how shell text editors such as
I know this can be done and i have seen it done using some
I'm currently working on a small script that needs to use gtk.StatusIcon() . For
I'm new to git and I have a moderately large number of weekly tarballs
I've written a shell script to soft-restart HAProxy (reverse proxy). Executing the script from
I've got a problem with my shell script. I am basically trying to create

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.