If your script requires user data during its run in order to make decisions on what to do, it can be tempting to prompt the user for each required bit of information when needed, perhaps using the -p option to the read builtin to request a prompt:
#!/bin/bash
read -p 'Do you want to create the directory? [y/N]: ' createdir
case $createdir in
y*|Y*)
mkdir -- "$HOME"/myscript || exit
;;
esac
This example will only create the directory named in the dir variable if a string such as y or YES (or yoyo!) is read from standard input, and assumes that it is likely to be the user's terminal.
This is convenient for interactive scripts, but it assumes that the user running the script is actually at a terminal, and it makes the script awkward to use in automation; someone trying to call this script automatically from cron or systemd would need to arrange to pipe in a single line, yes, to make it work. If the script needs to read from a data file at some point, it also complicates passing further data into it via the standard input.
If you need to make whether to do something during the script's run configurable, consider using a command-line option instead:
case $1 in
-c|--createdir)
mkdir -- "$HOME"/myscript || exit
shift
;;
esac
Now the need for the behavior can be specified on the command line:
$ myscript -c $ myscript --createdir
In some cases, it can even be desirable to put the variable in a configuration file, to be read at startup from a known path:
#!/bin/bash
if [[ -r $HOME/.myscriptrc ]] ; then
source "$HOME"/.myscriptrc
fi
case $createdir in
y*|Y*)
mkdir -- "$HOME"/myscript || exit
;;
esac
The preceding script uses source to run all of the Bash commands in the ~/.myscriptrc file, if it exists. If this configuration file contains a definition of createdir as anything starting with y or Y, the directory will be created:
$ cat ~/.myscriptrc createdir=yes
If, for whatever reason, your script really does need user input, which sometimes happens in secure contexts where you need to read straight from the terminal device, consider including an optional mode named --batch, --force, or a similar name that proceeds without that input, skipping all the prompts by assuming a sensible default for an answer:
#!/bin/bash
case $1 in -b|--batch) batch=1 shift ;; esac
if ((batch)) ; then createdir=y else read -p 'Do you want to create the directory? [y/N]: ' createdir fi
case $createdir in y*|Y*) mkdir -- "$HOME"/myscript || exit ;; esac