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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:25:53+00:00 2026-06-04T07:25:53+00:00

My problem can be simplified down to making the following script work (which takes

  • 0

My problem can be simplified down to making the following script work (which takes one command line argument):

#!/bin/bash
if ["$1" == "0"]; then
    echo "good"
else
    echo "bad"
fi

This should print good when I run script 0, but I can’t get it to. I’ve tried various combinations of quotes around the numbers, and I’ve tried =, ==, and -eq. So… bash, how does it work?

  • 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-04T07:25:54+00:00Added an answer on June 4, 2026 at 7:25 am

    The [ is actually a command. Do a ls /bin/[ or an ls /usr/bin/[. You’ll see it’s actually an executable file.

    The [...] is from the old Bourne shell days. The if command executes the statement, and if the exit code of that statement is a zero, the statement is considered true and the if clause is executed. If the exit code is not zero, the else clause is executed (if present).

    Try these:

    $ date
    Fri May 18 00:04:03 EDT 2012
    echo $?   #Prints the exit code of the date command
    0
    
    $ date -Q  #Shouldn't work, I hope...
    date: illegal option -- q
    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
        [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
    $ echo $?    #Exit code for the date command
    1
    

    You can see that date is a valid command and returns an exit code of 0 (the value of $?), but date -Q isn’t valid, and returns an exit code of 1.

    Now let’s try them in the if statement:

    if date
    then
       echo "I've successfully printed out the date!"
    else
       echo "I made a mistake in the command"
    fi
    

    Now try this:

    if date -q
    then
       echo "I've successfully printed out the date!"
    else
       echo "I made a mistake in the command"
    fi
    

    Originally, the [...] was an alias for the test command. The following are equivalent:

    if test -f /bin/ls    #Does a file called /bin/ls exist?
    then
       echo "There's a /bin/ls file"
    fi
    

    and

    if [ -f /bin/ls ]
    then
       echo "There's a /bin/ls file"
    fi
    

    This is why it’s very important to put spaces around the [ and ]. Because these are actually commands. In BASH, there’s built into the shell, but they are commands. That’s also why all the test parameters (things like -f, -z, and -eq) all are prefixed with dashes. They were originally parameters for the test command.

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

Sidebar

Related Questions

My problem can be summed up by making this simple command works : nice
Problem How can I do work in one thread and update a progressbar on
I have a problem which I've simplified down and have been scratching my head
I think that this problem can be sorted using reflection (a technology which I'm
I have a problem and can't solve it alone. My teacher gives me one
I have a problem in a VB.Net class library which I have simplified greatly
I'm running into some troubles with my code, which I narrowed down and simplified
Can anyone figure out a nice way to get the following code to work?
My database is quite complex so I've simplified my problem down to the tables
Seems like the slow Tomcat 7 startup problem can be resolved with metadata-complete set

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.