title: Bash programming: get name of one-and-only subfolder of given directory
body: <p>In <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a>, I'd like to get the name of the one-and-only subfolder of <code>a</code>:</p>
  
  <pre><code>cd /tmp; mkdir -p a/b; v=$(ls -1 a); echo "$v"
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>b
  </code></pre>
  
  <p>Which is what I wanted.</p>
  
  <p>This works, because I <em>know</em> there will be only one-and-only subfolder inside <code>a</code>.</p>
  
  <p>Feel free to assume this is guaranteed in all cases in my scenario.</p>
  
  <p>What I feel uneasy about, is this recommendation from <strong><a href="http://mywiki.wooledge.org/BashPitfalls" rel="nofollow">Bash pitfalls</a></strong></p>
  
  <pre class="lang-none prettyprint-override"><code>In addition to this, the use of ls is just plain unnecessary.
  It's an external command whose output is intended specifically
  to be read by a human, not parsed by a script.
  </code></pre>
  
  <p>This is even more intimidating and lapidary (from the same link as above):</p>
  
  <hr>
  
  <p><strong>parsing the output of ls -- a utility whose output should never ever be parsed.</strong></p>
  
  <hr>
  
  <p>It resonates with me but this, an <em>attempt</em> at using <a href="/questions/tagged/bash" class="post-tag" title="show questions tagged 'bash'" rel="tag">bash</a> globbing, obv. does not work:</p>
  
  <pre><code> cd /tmp; mkdir -p a/b; v=a/*; echo "$v"
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>a/*
  </code></pre>
  
  <p>Sadness.</p>
  
  <p>So, how can I get <em>globbing</em> for a variable assignment?</p>
  
  <p>I <em>think</em> I know how to leverage <em>globbing</em> outside of assignment..</p>
  
  <pre><code>for d in a/*; do echo "$(basename $d)"; done
  </code></pre>
  
  <p>outputs:</p>
  
  <pre><code>b
  </code></pre>
  
  <p>"The right answer" (TM).</p>
  
  <p>Could you kindly teach me how to <em>do this right</em> and explain the principle behind it too, please?</p>
  
  <p>** NOTE: I don't like the output of this command:**</p>
  
  <pre><code>cd /tmp; w='name with blanks'; mkdir -p "$w/b"; v=$(echo "$w"/*); echo "$v"
  </code></pre>
  
  <p>which is:</p>
  
  <pre><code>name with blanks/b
  </code></pre>
  
  <p>I'd like to just get:</p>
  
  <pre><code>b
  </code></pre>
  
  <p>do I <em>have to</em> use <code>basename</code> afterwards, or could I get <code>b</code> directly, somehow?</p>