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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:49:31+00:00 2026-05-23T16:49:31+00:00

In a bash script I’m writing, I use source to include the variable defined

  • 0

In a bash script I’m writing, I use source to include the variable defined in a configuration file. The script to be executed is act.sh, while the script to be sourced is act.conf.sh, so in act.sh I have:

source act.conf.sh

However this only works when running act.sh in the directory containing it, since act.conf.sh there refers to the file placed under the working directory. Is there a solution to make it refer to the file relative to the executing script without invoking cd? 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-23T16:49:32+00:00Added an answer on May 23, 2026 at 4:49 pm

    See: BASH FAQ entry #28: “How do I determine the location of my script? I want to read some config files from the same place.”

    Any solution isn’t going to work 100% of the time:

    It is important to realize that in the general case, this problem has no solution. Any approach you might have heard of, and any approach that will be detailed below, has flaws and will only work in specific cases. First and foremost, try to avoid the problem entirely by not depending on the location of your script!

    If you need to write a very reusable tool, then taking the correct path as a parameter to your script is going to be the most reliable method.

    Assuming your script is only going to be run from certain shells, and only with a little bit of flexibility required, you can probably relax some of this paranoia. It is still good to look at your options. There are common patterns that people use that are particularly problematic.

    In particular, the FAQ recommends avoiding the very commonly used $0 variable:

    Nothing that reads $0 will ever be bulletproof, because $0 itself is unreliable.

    As an alternative, you could use $BASH_SOURCE instead. Something like this:

    source "${BASH_SOURCE%/*}/act.conf.sh"
    

    There are some caveats to this solution, too. Check out the FAQ page to see the trade-offs between different solutions. They seem to recommend cd in combination with $BASH_SOURCE in cases where it will work for you, as you get a handy error condition when it fails to expand properly.

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

Sidebar

Related Questions

I am writing a bash script to search for a pattern in a file
In a bash script, I have to include the same file several times in
I am writing a bash script to deal with some installations in an automated
I have a bash script that creates a Subversion patch file for the current
I'm writing a bash script that needs to delete old files. It's currently implemented
I am writing a bash script to set up a production server. The tasks
This bash script concatenates the names for jar files to a classpath (variable CP),
I'm writing a bash script that needs to loop over the arguments passed into
I'm writing a Bash script that prints some text to the screen: echo Some
I am writing a bash script to run an integration test of a tool

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.