Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 76553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:41:29+00:00 2026-05-10T20:41:29+00:00

I have a bash shell script that loops through all child directories (but not

  • 0

I have a bash shell script that loops through all child directories (but not files) of a certain directory. The problem is that some of the directory names contain spaces.

Here are the contents of my test directory:

$ls -F test Baltimore/  Cherry Hill/  Edison/  New York City/  Philadelphia/  cities.txt 

And the code that loops through the directories:

for f in `find test/* -type d`; do   echo $f done 

Here’s the output:

 test/Baltimore test/Cherry Hill test/Edison  test/New York City test/Philadelphia 

Cherry Hill and New York City are treated as 2 or 3 separate entries.

I tried quoting the filenames, like so:

for f in `find test/* -type d | sed -e 's/^/\'/' | sed -e 's/$/\'/'`; do   echo $f done 

but to no avail.

There’s got to be a simple way to do this.


The answers below are great. But to make this more complicated – I don’t always want to use the directories listed in my test directory. Sometimes I want to pass in the directory names as command-line parameters instead.

I took Charles’ suggestion of setting the IFS and came up with the following:

dirlist='${@}' (   [[ -z '$dirlist' ]] && dirlist=`find test -mindepth 1 -type d` && IFS=$'\n'   for d in $dirlist; do     echo $d   done ) 

and this works just fine unless there are spaces in the command line arguments (even if those arguments are quoted). For example, calling the script like this: test.sh 'Cherry Hill' 'New York City' produces the following output:

 Cherry Hill New York City 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T20:41:30+00:00Added an answer on May 10, 2026 at 8:41 pm

    First, don’t do it that way. The best approach is to use find -exec properly:

    # this is safe find test -type d -exec echo '{}' + 

    The other safe approach is to use NUL-terminated list, though this requires that your find support -print0:

    # this is safe while IFS= read -r -d '' n; do   printf '%q\n' '$n' done < <(find test -mindepth 1 -type d -print0) 

    You can also populate an array from find, and pass that array later:

    # this is safe declare -a myarray while IFS= read -r -d '' n; do   myarray+=( '$n' ) done < <(find test -mindepth 1 -type d -print0) printf '%q\n' '${myarray[@]}' # printf is an example; use it however you want 

    If your find doesn’t support -print0, your result is then unsafe — the below will not behave as desired if files exist containing newlines in their names (which, yes, is legal):

    # this is unsafe while IFS= read -r n; do   printf '%q\n' '$n' done < <(find test -mindepth 1 -type d) 

    If one isn’t going to use one of the above, a third approach (less efficient in terms of both time and memory usage, as it reads the entire output of the subprocess before doing word-splitting) is to use an IFS variable which doesn’t contain the space character. Turn off globbing (set -f) to prevent strings containing glob characters such as [], * or ? from being expanded:

    # this is unsafe (but less unsafe than it would be without the following precautions) (  IFS=$'\n' # split only on newlines  set -f    # disable globbing  for n in $(find test -mindepth 1 -type d); do    printf '%q\n' '$n'  done ) 

    Finally, for the command-line parameter case, you should be using arrays if your shell supports them (i.e. it’s ksh, bash or zsh):

    # this is safe for d in '$@'; do   printf '%s\n' '$d' done 

    will maintain separation. Note that the quoting (and the use of $@ rather than $*) is important. Arrays can be populated in other ways as well, such as glob expressions:

    # this is safe entries=( test/* ) for d in '${entries[@]}'; do   printf '%s\n' '$d' done 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because NetFlix already has a really good recommendation engine. If… May 11, 2026 at 8:57 pm
  • Editorial Team
    Editorial Team added an answer so now, without even adding runat="server" to the title tag,… May 11, 2026 at 8:57 pm
  • Editorial Team
    Editorial Team added an answer I found this one: http://userscripts.org/scripts/review/3006 And this one also is… May 11, 2026 at 8:57 pm

Related Questions

I have this Shell script and I've managed to muck it up and I
I have a shell script that saves the output of a command that is
I have a that looks like this: I, [2009-03-04T15:03:25.502546 #17925] INFO -- : [8541,
I have a binary named A that generates output when called. If I call

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.