Search code examples
linuxbashldapopenldap

Bash command variable inside another command


I'm trying to execute a ldapsearch command inside a script.

Following does not work

ADMIN_USER="$(whoami)";

ldapmodify -h myldapserver  -p 23223 -D 'uid=$ADMIN_USER,ou=people,ou=company,dc=corpcom,dc=com' -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;

The above code gets executed with following error ldap_bind: Invalid credentials (49)

If hardcode the value like below, then it works.

ADMIN_USER="$(whoami)";

ldapmodify -h myldapserver -p 23223 -D 'uid=adminuser,ou=people,ou=company,dc=corpcom,dc=com' -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;

Is there a sepcific reason for this? how will i be able to fix this?


Solution

  • Bash isn't expanding the variable because you enclosed it in single quotes. Change the string to use double quotes:

    ADMIN_USER="$(whoami)";
    ldapmodify -h myldapserver -p 23223 -D "uid=${ADMIN_USER},ou=people,ou=company,dc=corpcom,dc=com" -w "$ADMIN_PWD" -f /tmp/reset_password.ldif.$PID;
    

    See also this answer.