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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:27:07+00:00 2026-05-23T12:27:07+00:00

if a script has if [ $1 == -? ]; then #line 4 echo

  • 0

if a script has

if [ $1 == "-?" ]; then #line 4
    echo "usage: ...."
fi

when the script get runs without any parameter, it will complain that

./script.sh: line 4: [: ==: unary operator expected

but if instead

if [ "$1" == "-?" ]; then #line 4
    echo "usage: ...."
fi

then everything’s fine

why is that?

thanks

  • 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-23T12:27:08+00:00Added an answer on May 23, 2026 at 12:27 pm

    If the first argument is missing or empty, your first script evaluates to:

    if [ == "-?" ] ; then
    

    … which is a syntax error. As you noticed, to prevent that you need to make use of "", then it evaluates to:

    if [ "" == "-?" ] ; then
    

    AFAIK this is due to the way the original Bourne shell was working. You should make it a habit of enclosing variables in "" to also work correctly with arguments that have spaces in it. For example, if you would call your script like this:

    ./myScript "first argument has spaces"
    

    Then your first script would evaluate to:

    if [ first argument has spaces == "-?" ] ; then
    

    which is also a syntax error. Also things like rm $1 will not do what you want if you pass filenames with spaces. Do rm "$1" instead.

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

Sidebar

Related Questions

I have a script that runs on the command line, called by a crontab.
I'm trying to create a Bash script that will extract the last parameter given
I have a script that has PHP on it, and a link that goes
I have A Script that has a Select statement to go to multiple sub
I have a python script that has to launch a shell command for every
I'm writing a script that has to move some file around, but unfortunately it
I have a PHP script that has a runtime of 34 seconds. But it
TextMate has a nice feature that allows you to execute a script from within
I'm writing a script that makes some trivial changes and then commits them to
I have a bash shell script which has the line: g=$(/bin/printf ${i}) when ${i}

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.