Server Fault is a question and answer site for system and network administrators. 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 am using ldapsearch with its -y option where the password for the ldap server is read in from a file in order to ensure that the password is not left in the command history.

ldapsearch requires that the password be in a file with no newlines. The only way I am aware of for doing this is:

echo -n "myreallysecretpassword" > /path/to/password.txt

but obviously that puts the password in the command history, which is what I'm trying to avoid.

I assume there's a better way, could someone point me in the right direction?

share|improve this question
If you're using bash, check your HISTCONTROL. Typically, if you prefix your command with a space, it will not be stored in your history. – jscott May 9 '11 at 12:58
That's very nice @jscott, thanks. – Eduardo Ivanec May 9 '11 at 13:00
Sadly our machines don't have HISTCONTROL set, and I can't really change those settings... – Rich May 9 '11 at 13:56
up vote 2 down vote accepted

Create the file however you want and just printf it without newline.

cat pw.txt | awk '{printf $1 }'


printf `cat pw.txt`
share|improve this answer
That works great, but it still means that the password appears in the process list if someone manages to view it at just the right time. In the end I have combined your answer with @Eduardo's: cat password.txt | awk '{printf $1}' > new_password.txt. – Rich May 9 '11 at 14:03

You can use read to input an intermediate variable semi-securely:

read -s tempvar # enter your password and press enter - it will not echo back
echo -n $tempvar > pw.txt
unset tempvar

Of course if you're worried mostly about the command history you can disable it in bash like this:

export HISTFILE=
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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