title: Listing directories with spaces using Bash in linux
body: <p>I would like to create a bash script to list all the directories in a directory provided by the user via input, or all the directories in the current directory (given no input).</p>
  
  <p>Here's what I have thus far, but when I execute it I encounter two problems.</p>
  
  <p>1) The script completely ignores my input. The file is located on my desktop but when I type in "home" as the input, the script simply prints the directories of the Desktop (current directory).</p>
  
  <p>2) The directories are printed on their own lines (intended) but it treats each word in a folder name as its own folder. i.e.  is printed as:</p>
  
  <pre><code>this
  folder
  </code></pre>
  
  <p>Here's the code I have so far:</p>
  
  <pre><code>#!/bin/bash
  
  echo -n "Enter a directory to load files: "
  read d
  
  if [ $d="" ]; #if input is blank, assume d = current directory
  then d=${PWD##*/} 
  for i in $(ls -d */);
  do echo ${i%%/};
  done
  else #otherwise, print sub-directories of given directory
  for i in $(ls -d */);
  do echo ${i%%/};
  done
  fi
  </code></pre>
  
  <p>Also in your response please explain your answer as I'm very new to bash. </p>
  
  <p>Thanks for looking, I appreciate your time.</p>
  
  <p>EDIT: Thanks to John1024's answer, I came up with the following:</p>
  
  <pre><code>#!/bin/bash
  
  echo -n "Enter a directory to load files: "
  IFS= read d
  ls -1 -d "${d:-.}"/*/
  </code></pre>
  
  <p>And it does everything I need. Much appreciated!</p>