Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to delete old network user accounts off our lab server (Mac OS X 10.6). I found a simple command line command to find user directories that have not been accessed for over a year:

find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356

Running this command successfully lists all of the old accounts. I then found what looks like a simple for loop, but it doesn't work:



USERLIST='find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356'

for a in $USERLIST ; do
  [[ "$a" == "$keep1" ]] && continue                    #skip account 1
  [[ "$a" == "$keep2" ]] && continue                    #skip account 2
  echo "Deleting account and home directory for" $a
  dscl . delete $a                                      #delete the account
  rm -r $a                                              #delete the home directory

When I run this command as an executable shell file or just straight in the command line, it loops through $a as being each word in the $USERLIST. In other works if I simply do this:

USERLIST='find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356'

for a in $USERLIST ; do 
   echo $a

It will return:


whereas I would expect it to return each directory that meets the "find" command.

I need to figure out how to make an array of the results from the find command to loop through. I guess I simply don't understand how the arrays and strings are handled in bash. Any thoughts?

share|improve this question
up vote 0 down vote accepted

You have enclosed your find command in the USERLIST assignment in single quotes – that will assign the whole command as a string to the $USERLIST variable (foregoing all shell expansion – that’s what single quotes are for). Instead, to get the command output assigned, you need to enclose it in backticks (`) or in a $(<command>) construct, i.e.

USERLIST=`find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356`


USERLIST=$(find /Volumes/Server-HD2/NetworkUsers/Students -type d -maxdepth 1 -mindepth 1 -not -name "." -mtime +356)
share|improve this answer
Thanks! Maybe the original source and the backtick and it somewhere got typed or copied as a single quote ('). – James Pierce Nov 3 '11 at 21:10
Backticks are confusing in several respects. Just use $( ) instead. – Gordon Davisson Nov 4 '11 at 2:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .