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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:32:02+00:00 2026-05-30T11:32:02+00:00

I want to echo the PATH variable, with the goal to get the same

  • 0

I want to echo the PATH variable, with the goal to get the same output from a Java ProcessBuilder as running echo $PATH in the terminal. However, when it executes the output is actually $PATH instead of the value of the PATH variable. I wonder if ProcessBuilder is escaping the $ and is there a trick to prevent this?

Here is a code sample of what I am talking about that outputs the string “$PATH”:

List<String>  processBuilderCommand = ImmutableList.of("echo","$PATH");

ProcessBuilder processBuilder = new ProcessBuilder(processBuilderCommand).redirectErrorStream(true);

final Process process = processBuilder.start();

String commandOutput = CharStreams.toString(CharStreams.newReaderSupplier(new InputSupplier<InputStream>() {
                @Override
                public InputStream getInput() throws IOException {
                    return process.getInputStream();
                }
            }, Charset.defaultCharset()));

System.out.println(commandOutput);

Some extra context:

I am trying to simulate the sort command not being found for one of my unit tests. I am using this hack/trick to change my PATH and by inspecting the result of processBuilder.environment() and sure enough the PATH variable being passed to the process shouldn’t allow finding sort (I’ve tried the empty string as well as a random path). I’d like to see if the shell is doing anything funny and fixing back up PATH which I am trying to destroy.

  • 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-30T11:32:04+00:00Added an answer on May 30, 2026 at 11:32 am

    $PATH is the syntax used in bash (and other shells) to refer to the environment variable PATH. Since it’s echo you execute using the ProcessBuilder, and not bash it’s not very surprising that it doesn’t print the content of the environment variable.

    You should either get hold of the content of the environment variable from Java, and give it as argument to the external process, or, execute a program which is capable of interpreting the $PATH syntax properly, (such as bash).


    As pointed out in your comment below,

    [...]  ImmutableList.of("/bin/bash","-c","echo $PATH")  [...]
    

    indeed prints the content of the PATH environment variable.

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

Sidebar

Related Questions

Following How to get folder path from file path with CMD I want to
I have two pages, A and B. I want to echo a variable written
I want to determine what the PATH environment variable was after I execute a
I want to set a path variable cp to one of two values depending
I want to assign the output of a shell command into a make variable.
I want to run Batch file 1(Path C:\3rdparty\Test\App) from Batch file 2(C:) and VS2008.sln
I want to do formatting using echo in shell scripting. Here is a small
when it gives a error I want him to do 2 things. echo nl2br($qVraagOp);
I have a shell script with a lot of echo statements. I want to
I have a command, for example: echo "word1 word2" . I want to put

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.