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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:09:58+00:00 2026-05-12T17:09:58+00:00

I need to understand bash if expressions. Sure, I’ve used Google. I know that

  • 0

I need to understand bash if expressions. Sure, I’ve used Google. I know that it goes something like this:

if [ expr operator expr ]
then
doSomeThing
fi

However, I understand that Bash doesn’t have a boolean data type. I want to check if the file passed as an argument ($1) exists. The way I would do this straight of my mind:

if [ -e $1 = true ] 
then 
echo "File exists."
fḯ

Or maybe like:

if [ -e $1 ] #Assuming that this is true only if file in $1 exists. 

None of these work, and I’m not sure what [ ] means. -e $1 seems like a smart choice, but it is always true? There are different operators for strings and integers. And I can’t use parenthesis to group together expressions. This is so confusing.

Anyone got a few hints? The IF in bash does not work like if I’ve tried in any other language.

  • 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-12T17:09:58+00:00Added an answer on May 12, 2026 at 5:09 pm

    [ … ]

    means that the program /usr/bin/test gets executed with ... as the arguments and its return value is checked (0 means true and x != 0 means false (yes, 0 really means true here because 0 is the OK exit code in UNIX)). So

    if [ -e $1 ]; then
        echo "ok"
    fi
    

    is correct. It’s the same as

    if test -e $1; then
        echo "ok"
    fi
    

    The sole problem is that $1 could contain spaces. If so /usr/bin/test gets confused, since it gets 3 or more arguments. The first one (-e) is a unary operator (wants one argument). Since you gave it 2 or more (one /usr/bin/test argument is the -e itself), it complains like that:

    ...: binary operator expected
    

    So, simply use

    if [ -e "$1" ];then
        echo "ok"
    fi
    

    That will even work if $1 contains spaces. Another possibility is to use

    if [[ -e $1 ]]; then
        echo "ok"
    fi
    

    That will do the same but it gets evaluated by bash itself and no /usr/bin/test program gets forked.

    Compounds are as usual but -a means and and -o means or. So

    if [ -e /etc/fstab -a -e "$1" ]; then
       echo "ok"
    fi
    

    will echo ok if /etc/fstab and the file given as first command line argument exist.

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

Sidebar

Related Questions

No related questions found

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.