automated terminal push
All checks were successful
code.softwareshinobi.com-learn/docker.softwareshinobi.com/pipeline/head This commit looks good
All checks were successful
code.softwareshinobi.com-learn/docker.softwareshinobi.com/pipeline/head This commit looks good
This commit is contained in:
54
docs/.Bash-Scripting/005-bash-user-input.md
Executable file
54
docs/.Bash-Scripting/005-bash-user-input.md
Executable file
@@ -0,0 +1,54 @@
|
||||
# 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!
|
||||
Reference in New Issue
Block a user