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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:43:24+00:00 2026-05-15T11:43:24+00:00

In a BASH script, I’m trying to detect whether a file exists. The filename

  • 0

In a BASH script, I’m trying to detect whether a file exists. The filename is in a variable but the -e command seems to be unable to detect the file. The following code always outputs “~/misc/tasks/drupal_backup.sh does not exist”

filename="~/misc/tasks/drupal_backup.sh"

if [ -e "$filename" ]; then
  echo "$filename exists"
else 
  echo "$filename does not exist"
fi

On the other hand, the following code detects the file correctly:

if [ -e ~/misc/tasks/drupal_backup.sh ]; then
  echo "$filename exists"
else 
  echo "$filename does not exist"
fi

Why would this be? How can I get it to detect the file when the filename is in a variable?

  • 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-15T11:43:25+00:00Added an answer on May 15, 2026 at 11:43 am

    That’s an interesting one. Substituting $HOME for ~ works as does removing the quotes from the assignment.

    If you put a set -x at the top of that script, you’ll see that the version with quotes sets filename to ~/... which is what’s given to -e. If you remove the quotes, filename is set to the expanded /home/somebody/.... So in the first case, you see:

    + [ -e ~/... ]
    

    and it doesn’t like it. In the second case, you see:

    + [ -e /home/somebody/... ]
    

    and it does work.

    If you do it without the variable, you see:

    + [ -e /home/somebody/... ]
    

    and, yes, it works.


    After a bit of investigation, I’ve found that it’s actually the order in which bash performs its expansions. From the bash man page:

    The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.

    That’s why it’s not working, the variable is substituted after the tilde expansion. In other words, at the point where bash wants to expand ~, there isn’t one. It’s only after variable expansion does the word get changed into ~/... and there will be no tilde expansion after that.

    One thing you could do is to change your if statement to:

    if [[ -e $(eval echo $filename) ]]; then
    

    This will evaluate the $filename argument twice. The first time (with eval), there will be no ~ during the tilde expansion phase but $filename will be changed to ~/... during the variable expansion phase.

    Then, on the second evaluation (the one being done as part of the if itself), the ~ will be there during the tilde expansion phase.

    I’ve tested that on my .profile file and it seems to work, I suggest you confirm in your particular case.

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

Sidebar

Ask A Question

Stats

  • Questions 447k
  • Answers 447k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $(this).find('.productMenuHolder').show() I suggest you look a bit closer at the… May 15, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer Just eval it using whatever library is relevant to the… May 15, 2026 at 7:35 pm
  • Editorial Team
    Editorial Team added an answer I got this problem resolved. This occured only in Simulator… May 15, 2026 at 7:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.