![](https://seccdn.libravatar.org/avatar/b12cfb65ca4faebc3e3aac17838e8f8d.jpg?s=120&d=mm&r=g)
Jason, On Friday 19 November 2004 11:54, Jason Joines wrote:
I'm trying to do an ldapsearch in a bash script. I need to be able to expand a variable and use a NOT (!).
If I use double quotes around the filter like this: ldapsearch "(&(cn=$LOGIN)(!(usertype=student)))" bash complaines about "bash: !: event not found" and doesn't even execute the command.
If I use single quotes around the filter like this: ldapsearch '(&(cn=$LOGIN)(!(usertype=student)))' the command runs but the variable does not get expaned.
If I use a backslash escape like this: ldapsearch "(&(cn=$LOGIN(\!(usertype=student)))" ldapsearch complains about "ldapsearch: ldap_search_ext: Bad search filter (-7)"
Any ideas?
I'm completely unfamiliar with LDAP and related tools, but I can tell you about BASH quoting and escaping: - You can mix and match quoting styles. If parts of the argument are laden with character special within double quotes ($ or !, e.g.), enclose those parts within single quotes (a.k.a. apostrophes) and switch to double quotes when you need variable interpolation or history expansion. In order to produce a single argument, leave no space between the two (or more) differently quoted segments. - Replace your command with echo to see what's being passed: % echo "(&(cn=$LOGIN(\!(usertype=student)))" (&(cn=(\!(usertype=student))) So, now you can see that the escape that prevented history expansion got passed on to ldapsearch, too. Presumably this command will do what you want: % ldapsearch "(&(cn=$LOGIN)"'(!(usertype=student)))'
Jason Joines
Randall Schulz