Files
bash.softwareshinobi.com/docs/005-bash-user-input.md
Software Shinobi 4f7705494b
All checks were successful
learn org at code.softwareshinobi.com/bash.softwareshinobi.com/pipeline/head This commit looks good
automated terminal push
2025-06-05 19:36:22 -04:00

54 lines
1.2 KiB
Markdown

# Bash User Input
With the previous script, we defined a variable, and we output the value of the variable on the screen with the `echo $name`.
Now let's go ahead and ask the user for input instead. To do that again, open the file with your favorite text editor and update the script as follows:
```bash
#!/bin/bash
echo "What is your name?"
read name
echo "Hi there $name"
echo "Welcome to DevDojo!"
```
The above will prompt the user for input and then store that input as a string/text in a variable.
We can then use the variable and print a message back to them.
The output of the above script would be:
* First run the script:
```bash
./devdojo.sh
```
* Then, you would be prompted to enter your name:
```
What is your name?
Bobby
```
* Once you've typed your name, just hit enter, and you will get the following output:
```
Hi there Bobby
Welcome to DevDojo!
```
To reduce the code, we could change the first `echo` statement with the `read -p`, the `read` command used with `-p` flag will print a message before prompting the user for their input:
```bash
#!/bin/bash
read -p "What is your name? " name
echo "Hi there $name"
echo "Welcome to DevDojo!"
```
Make sure to test this out yourself as well!