Bash prompt


Read

Prompt user in Bash:

echo -n 'Yes or no? [Y/n]: '
read input

Control Flow

Print no when input begins with n:

if [[ $input =~ 'n' ]]; then
  echo 'no'
fi

Print Yes when input does not begin with n:

if ! [[ $input =~ 'n' ]]; then
  echo 'Yes'
fi

Control flow that handles both Yes and no cases:

if ! [[ $input =~ 'n' ]]; then
  echo 'Yes'
else
  echo 'no'
fi

Script

Here’s the full script:

echo -n 'Yes or no? [Y/n]: '
read input

if ! [[ $input =~ 'n' ]]; then
  echo 'Yes'
else
  echo 'no'
fi

Demo

Repl.it:



Please support this site and join our Discord!