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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:58:29+00:00 2026-05-11T05:58:29+00:00

In a bash script, I have to check for the existence of several files.

  • 0

In a bash script, I have to check for the existence of several files.

I know an awkward way to do it, which is as follows, but that would mean that my main program has to be within that ugly nested structure:

if [ -f $FILE1 ] then if [ -f $FILE2 ] then   echo OK   # MAIN PROGRAM HERE fi fi 

The following version does not work:

([ -f $FILE1 ] && [ -f $FILE2 ]) ||  ( echo 'NOT FOUND'; exit 1 ) echo OK 

It prints

NOT FOUND OK 

Is there an elegant way to do this right?

UPDATE: See the accepted answer. In addition, in terms of elegance I like Jonathan Leffler’s answer:

arg0=$(basename $0 .sh) error() {     echo '$arg0: $@' 1>&2     exit 1 }  [ -f $FILE2 ] || error '$FILE2 not found' [ -f $FILE1 ] || error '$FILE1 not found' 
  • 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. 2026-05-11T05:58:29+00:00Added an answer on May 11, 2026 at 5:58 am

    How about

    if [[ ! ( -f $FILE1 && -f $FILE2 ) ]]; then     echo NOT FOUND     exit 1 fi  # do stuff echo OK 

    See help [[ and help test for the options usable with the [[ style tests. Also read this faq entry.

    Your version does not work because (...) spawns a new sub-shell, in which the exit is executed. It therefor only affects that subshell, but not the executing script.

    The following works instead, executing the commands between {...} in the current shell.

    I should also note that you have to quote both variables to ensure there is no unwanted expansion or word splitting made (they have to be passed as one argument to [).

    [ -f '$FILE1' ] && [ -f '$FILE2' ] || { echo 'NOT FOUND'; exit 1; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Bash script that repeatedly copies files every 5 seconds. But this
I have bash script that work for many files in loop. It compiles, check
I have a bash script which I would like to handle spaces. I know
I have a bash script that I am run to check to see if
I have a bash script that looks like the following: #!/bin/bash FILES=public_html/*.php # */
I have a share backup bash script that works as is, but it uses
I have a bash script in which i check the exit code of a
I'm writing a bash script and I have errexit set, so that the script
So I have a bash script that needs to take an arbitrary number of
Currently I have a bash script which runs the find command, like so: find

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.