Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

125
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda