There are a few different ways your bash program can accept user input
- Using Command Line Arguments
- Prompt for input during script execution
Accepting User Input with Command Line Arguments
When you run a command on the command line, you can supply arguments to them that act as inputs for that command.
For instance, when you run ls -l /Documents, the `-l` and `/Documents` are both command line arguments to the command `ls`.
You can do the same thing while writing your own bash scripts. For example, by passing "Amit" and "dog" as positional parameter arguments -
`bash print-name-animal.sh amit dog`

When you run this command, bash will automatically provide the following positional parameter variables to our script, each containing the contents of the command line.
- $0 - The variable $0 is set to the first word of the command line statement - the name of your bash script. In this case, it will be print-name-animal.sh
- $1 - First parameter after the command name, in this case it will be set to amit
- $2 - Second parameter after the command name, in this case it will be set to dog
If there was a third parameter, its value would be placed in the variable $3, and so on.
These shell variables are automatically set by the shell when we run our script so all we need to do is refer to them in our script, like:
print-name-pet.sh

But, what if you had more than 9 parameters?
Parameters greater than 9 can be accessed using curly braces around the number; for instance, ${10} would be the tenth parameter, and ${123} would be the 123rd.
Did you know?
You can also check the number of positional parameters (excluding $0) by using the variable $#.
You can also access all the items on the command line starting at the first parameter ($1), with the special variable $@.


Best Practices
Positional parameters are delimited by a space. The shell interprets the things after the spaces as individual parameters. If the parameter itself contains a space, enclose it in quotation marks, like "Snow Leopard".
Prompt for input using read
In addition to using command line arguments as input, you can also prompt the user for input during the script execution. You do that using the read command.
The syntax for the read command is
read-p "PROMPT" VARIABLE_NAME

Here’s an example for using it ask the user for their name and favorite pet:

How it works
The read command here captures the input from the user and stores it in our variables - $first_name and $favorite_animal
Read Command Options
The read command provides a few options/flags you can use:
- -p Prompt: You can use this to provide a helpful message for the user before the input prompt. For example, read -p “What’s your height (inches)?” user_height
- -s Silent Mode: You can use this to accept invisible input from the user. Useful for passwords or other secret information.
- -t Timed Input: You can use this to set time in seconds that your script should wait for taking input from the user.
Choosing Command Line Arguments vs Prompting for execution
Accept user input through command line arguments (preferred, stays in history, making it easier for users to run the command again, without waiting for manual typing for input)