Jump to content

Bash and heredoc


Recommended Posts



I'm really green when it comes to bash...


Trying to create a script which will SSH into multiple servers and report back a list showing the status of a deployment on each.  The intention is for the script to run from my local machine.


I read using heredoc is the way to go but I don't think its possible to store variables which are accessible outside of the sub-session - i.e. you can't store variables inside heredoc and have them accessible outside


So my question is does anyone know a good way to get the variables from heredoc into the 'higher level' bash script so I can aggregate the results from each server?


Pseudo code to illustrate what I'm aiming for:

ssh me@ << EOF
# Get server Status to variable

ssh me@ << EOF
# Get Server Status to variable

Link to comment
Share on other sites

Generally speaking you just capture the output of the command and store that into a variable. That would look something like this:

STATUS=$(ssh you@example.com whatever-command)
So if you wanted to get the disk usage for various servers you might do:

SERVER1=$(ssh you@server1.example.com df -h)
SERVER2=$(ssh you@server2.example.com df -h)
SERVER3=$(ssh you@server3.example.com df -h)

echo Usage for server 1:
echo $SERVER1;
echo Usage for server 2:
echo $SERVER2;
echo Usage for server 3:
echo $SERVER3;
Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.