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

562
Vistas
bash variable seems to fail assignment to output of psql executed in container

I have Postgres running in a docker container on a remote host. I'm attempting to ssh to that remote host, and use a heredoc to pass a script to that shell. In that script, I attempt to execute a psql query against the postgres database that resides in the container on that host, and assign the output to a variable. I'm sorry, I know that's a bit convoluted, but maybe some code will be more coherent (nor not)

If I run this, I get nothing out of my "echo" of $dbVersion... in fact, due to some other experimentation, I'm fairly convinced it never gets to the "echo" line at all, but for the life of me I don't know why, because...

ssh root@$serverIP13 <<EOF
postgresId=\$(docker ps | grep 'postgres_db' | awk '{print \$1}')
dbVersion=\$(docker exec -i \$postgresId psql -X -A -d myDb  -U myUser -t -c "SELECT Max(Version) FROM DbVersion;")
echo \$dbVersion
EOF

...after turning and twisting my code this way and that... I finally found a way that works, and I get a successful "echo" of my version number. I'm quite new to bash, so I'm hoping someone out there can discern this mystery for me and help me understand a bit better how bash works in this regard, and why the below succeeds, and the above fails.

ssh root@$serverIP13 <<EOF
postgresId=\$(docker ps | grep 'postgres_db' | awk '{print \$1}')
dbVersion=\$(echo "psql -X -A -d myDb -U myUser -t -c 'SELECT Max(Version) FROM DbVersion;'" | docker exec -i \$postgresId bash)
echo \$dbVersion
EOF
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The normal way to get a docker exec -i output is to call for docker to run a shell (which, in turn, runs your command.
See "Bash / Docker exec: file redirection from inside a container" (I mentioned in 2015)

dbVersion=\$(docker exec -i sh -c '\$postgresId psql -X -A -d myDb  -U myUser -t -c "SELECT Max(Version) FROM DbVersion;"')
over 4 years ago · Santiago Trujillo Denunciar

0

Following script worked without invoking bash :

ssh root@$serverIP13 << 'EOF'
postgresId=$(docker ps | grep 'postgres_db' | awk '{print $1}')
dbVersion=$(docker exec $postgresId psql -X -A -d postgres -U postgres -c "select current_database()")
echo $dbVersion
EOF

I use 'EOF' to avoid having to escape.

To pass local values to remote :

ssh root@$serverIP13 /bin/bash -s 1 2 3 << 'EOF'
echo $1 $2 $3
EOF

You should not use -i if you put docker exec in $(...)

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