I am trying to generate some PATH-like directory list, using simple bash- and UNIX commands. I think this one here gives me the output I like to use:

$ find /usr/local -type d -exec dirname \{\} \; | sort -u | tr '\n' ':'

However, when I try to use this expression using command substitution (e.g. for assigning to a variable), it breaks:

$ echo $(find /usr/local -type d -exec dirname \{\} \; | sort -u | tr '\n' ':' )
/usr /usr/local /usr/local/lib /usr/local/lib/python2.6 /usr/local/lib/site_ruby /usr/local/lib/site_ruby/1.8 /usr/local/lib/xemacs /usr/local/share /usr/local/share/emacs /usr/local/share/emacs/23.2 /usr/local/share/sgml /usr/local/share/xml /usr/local/share/zsh

Any ideas what I am doing wrong, or how I can achieve my goal differently?

  • 1
    Cannot reproduce, neither in bash nor zsh. Both produce the same output for me. – slhck Jul 30 '12 at 14:31
  • Hm, I am using Bash 4.1.5 on Debian Squeeze. Maybe there's some shopt which triggers this? – Arne Jul 30 '12 at 14:53
  • I'm on 4.2.24 with Ubuntu. Have you set any custom options in one of your startup scripts? – slhck Jul 30 '12 at 15:00
  • Funny enough this happens for me only in interactive shells. When I put this in a script, it works :-/ – Arne Jul 31 '12 at 7:19
  • Interesting. Found any differences in your bashrc or bash_profile that could influence the behavior? – slhck Jul 31 '12 at 7:21

Check the contents of the environment variable IFS, the field separator. It is not used in the find form, but in the echo $(find) form.

export IFS="-"
echo $(...)


export IFS=:
echo $(echo asdf:asdf:asdf)
> asdf asdf asdf

export IFS=;
echo $(echo asdf:asdf:asdf)
> asdf:asdf:asdf

export IFS=:
unset IFS
echo $(echo asdf:asdf:asdf)
> asdf:asdf:asdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.