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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:23:05+00:00 2026-06-07T14:23:05+00:00

I have a bash file (let’s call it bash_file) which contains a number of

  • 0

I have a bash file (let’s call it bash_file) which contains a number of environment variable assignments (e.g. TOOLS_DIR="/opt/tools") as well as bash code which sources another file (e.g. source /opt/other_file) and constructs environment variables from the environment variables sourced from other_file (e.g. NEW_ENV_VAR=$FILEPATH/$FILENAME where FILEPATH and FILENAME are assigned in other_file).

I have a Java application which requires visibility of these environment variables, but using the standard System.getenv() doesn’t pick up these environment variables since the bash file hasn’t been sourced in Java’s shell yet. If I use Java’s Runtime.exec("source bash_file"), then the environment variables are only present in Java’s child shell, even if bash’s export command is used.

If I were in pure bash, I’d simply perform my source bash_file and I’d have access to all the environment variables assigned in bash_file, but it’s not that simple in Java.

I have thought of a few options already, none of which seem particularily elegant:

  1. In the same process with the source bash_file command, add the current env to the .bashprofile so that it will be available for the all future runtime exec calls, such as System.getenv().

    Problem: I’m not 100% sure that the getenv will even pick up on env vars added to the bashprofile during runtime, and having all of these variables exported globally isn’t nice at all.

  2. Again in the same process as source_bash_file, save current env to a text file, and manually parse with Java.

    Problem: This would get the job done, but it’s nowhere near as nice and elegant as the Map returned by System.getenv(). Writing the parser in Java also seems quite tricky.

  3. Figure out where Java’s bash environment is instantiated (likely in java.System.lang) and modify in order to perform the source bash_file before initializing Java’s environment, so the variables are inheireted by my Java app.

    Problem: This one seems like the messiest of all my options, and I’d have no idea where to begin looking for the initialization of Java’s shell environment.

Is there any way to perform a bash-like source operation from Java, or alternatively, what is the simplest implementation for obtaining that functionality? I have also voiced a number of uncertainties in my evaluation of possible courses of action, and I would appreciate it if anyone could fill me in on those uncertainties.

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-06-07T14:23:07+00:00Added an answer on June 7, 2026 at 2:23 pm

    No, there’s no general method for running bash code inside Java. But it’s very common for Java programs to be invoked via shell script; are you sure yours doesn’t already work that way? If it does, that’s the most straightforward place to source the other bash file before executing your program.

    If that’s not an option for some reason, then #2 is the best solution. #1 and #3 are no good; you don’t want to be changing system-wide or account-wide settings just to be able to run this program- especially if you ever plan to have other people use it.

    The parsing for the #2 option doesn’t need to be very hairy. Just output the environment variables using the set builtin, or something similar, and in your Java parser simply read one line at a time and split on the first = sign on each line. If a variable contains a newline, bash should escape it inside a dollar-quoted string, which would probably confuse your parsing but you probably don’t need to worry about embedded newlines, so treat the result as garbage. You also probably want to dequote values contained in '' or "", and maybe even handle backslash escapes in that second case, but in most cases like this the backslash-parsing wouldn’t be necessary.

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

Sidebar

Related Questions

I have to put in a bash variable the first line of a file.
I have a shell script file (run.sh) that contains the following: #!/bin/bash %JAVA_HOME%/bin/java -jar
I have a bash-script (let's call it /usr/bin/bosh ) using the following she-bang line:
I have a bash file, which I’m trying to run in Cygwin on a
I have this bash file, which asks for IP, password, etc. for OpenSSH to
Say I have a file at the URL http://mywebsite.example/myscript.txt that contains a script: #!/bin/bash
I have a little program, let's call it program by simplicity which has normal
I currently have a Bash completion file which completes a single parameter from a
I have a bash script file which starts with a function definition, like this:
I have an application, let's call it myapp.exe, which is dual-mode console/GUI, built as

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.