Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Bash variable assignment strange behaviour

I am trying to write a bash script, while doing that am stuck here:

I do not understand why this works:

MSG=$(pwd)
echo $MSG

Output:

/home/harsh/source/git/trunk

BUT this does not:

MSG=$(java -version)
echo $MSG

Output:

BLANK

Please help!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Some commands might need 2>&1 at the end to get any output:

MSG=$(java -version 2>&1)

It sends any standard error(2) to wherever standard output(1) is redirected.

over 4 years ago · Santiago Trujillo Report

0

Error messages are typically written to the standard error stream stderr instead of the standard output stream stdout. If java -version generates an error instead of what you expected (printing the version), it will likely do so to stderr. It is also possible that the version information could also printed to stderr.

The command substitution $() takes the output from stdout of what's inside the $() and substitutes it in its place. In case of an error, this could be nothing. If you are typing this from a terminal, you should still see any output (e.g. error messages) from java's stderr in the terminal.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!