title: BASH: read in while loop
body: <pre><code>while [ $done = 0  ]
    do
    echo -n "Would you like to create one? [y/n]: "
    read answer
    if [ "$(answer)" == "y" ] || [ "$(answer)" == "Y" ]; then
      mkdir ./fsm_$newVersion/trace
      echo "Created trace folder in build $newVersion"
      $done=1
    elif [ "$(answer)" == "n" ] || [ "$(answer)" == "N" ]; then
      $done=2
    else
      echo "Not a valid answer"
    fi
  done
  </code></pre>
  
  <p>Ok so I have this simple bashscript above that simply just tries to get input from a user and validate it. However I keep getting this error </p>
  
  <pre><code>./test.sh: line 1: answer: command not found
  ./test.sh: line 1: answer: command not found
  ./test.sh: line 1: answer: command not found
  ./test.sh: line 1: answer: command not found
  </code></pre>
  
  <p>Which I have no idea why because "answer" is nowhere near line 1. So I ran into this <a href="http://fvue.nl/wiki/Bash:_Piped_%60while-read&#39;_loop_starts_subshell" rel="nofollow noreferrer">article</a></p>
  
  <p>Which makes sense since it's referring to line 1 and can't find answer. So it seems to be starting a new subshell. However I didn't really understand the solution and can't see how I would apply it to my case. I just wanna get this to work.</p>