title: Bash extract user for a particular host from ssh config file
body: <p>I'm writing a bash script where I need to obtain a particular user from an ssh config file. The ssh config file looks a little something like this: </p>
  
  <pre><code>HOST blag
      HostName blag.net.au
      Port 2683
      User blaguser
  Host bloo
    User ABCDEF
    IdentityFile ~/.ssh/id_rsa
  HOST biff
      HostName biff.net.au
      Port 2683
      User biffuser
  </code></pre>
  
  <p>I want to obtain the string 'ABCDEF' and put it in a variable, by searching for Host bloo.</p>
  
  <p>I was able to use the answer at <a href="https://superuser.com/questions/791374/get-variable-values-from-config-file/791387#791387?newreg=6626dd5535194d0180a91b6ace31e16f">https://superuser.com/questions/791374/get-variable-values-from-config-file/791387#791387?newreg=6626dd5535194d0180a91b6ace31e16f</a> to read the config file but it assigns the array with the last host entry in the file.</p>
  
  <p>I'm able to delete the host entry with this answer <a href="https://stackoverflow.com/questions/36110829/how-can-i-remove-a-host-entry-from-an-ssh-config-file">How can I remove a Host entry from an ssh config file?</a>. The sed command here could be edited to extract the correct <code>User</code> but I'm not sure precisely how</p>
  
  <p>I'm having a lot of trouble with it. Can anyone assist? An answer which uses sed would be preferable.</p>