1

Using a bash script, how do you save information in a file, without printing it on the console? I've written a script that requires the user to enter a value. This value has to be logged in a text file, without printing it on screen. So far I've written:

echo "x = " 
read v_x 
echo $v_x > temp.txt 

but that prints it on the screen as well as saving it in the file.

George Georgiev
  • 193
  • 1
  • 1
  • 6
  • So you mean when user writes it you do not want it to appear in screen? – fedorqui Nov 12 '13 at 09:04
  • 1
    possible duplicate of [How do I echo stars (\*) when reading password with \`read\`?](http://stackoverflow.com/questions/1923435/how-do-i-echo-stars-when-reading-password-with-read) – Claudio Nov 12 '13 at 09:06
  • possible duplicate of [How to make bash script ask for a password?](http://stackoverflow.com/questions/2654009/how-to-make-bash-script-ask-for-a-password) – legoscia Nov 22 '13 at 14:08

1 Answers1

2

You can use the -s option for read:

read -s v_x

help read tells:

  -s        do not echo input coming from a terminal
devnull
  • 118,548
  • 33
  • 236
  • 227
  • 1
    No, this isn't what my problem is. I don't want to hide what the user is typing, while he's typing it. The problem, I think, lies with the `echo` command. The line `echo $v_x > temp.txt` prints on the console screen as well as in the file, so the number the user just entered get repeated on screen after he hits 'return'. – George Georgiev Nov 13 '13 at 12:14
  • @GeorgeGeorgiev Saying `echo $v_x > temp.txt` redirects the `STDOUT` to the file `temp.txt`. That would __not__ produce any output on the terminal. – devnull Nov 13 '13 at 12:22
  • Well, that's what I thought too. I have no idea why it happens but it does repeat the same number and its not from the `read`. Thanks anyway. I found a way around that problem. – George Georgiev Nov 13 '13 at 20:04