bash user input if

frnhr picture frnhr · Apr 4, 2011 · Viewed 52.5k times · Source

I am tring to do simple

Do you want to do that? [Y,n] _

question in bash.

i tried

echo "Do that? [Y,n]"
read DO_THAT
if ["DO_THAT"="y"]; then
  do_that
fi

but it fails: bash: [y=y]: command not found

what am I doing wrong??!

Answer

user unknown picture user unknown · Apr 4, 2011

You might consider explicit prompting: -p and specifying 1-character-input -n1 which allows to insert y without ENTER.

read -n1 -p "Do that? [y,n]" doit 
case $doit in  
  y|Y) echo yes ;; 
  n|N) echo no ;; 
  *) echo dont know ;; 
esac