Bash User Input
With the previous script, we defined a variable and we output the value of the variable on the screen with the
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:
#!/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:
- Then you would be prompted to enter your name:
What is your name? Bobby
- Once you've typed your name just hit enter:
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:
#!/bin/bash read -p "What is your name? " name echo "Hi there $name" echo "Welcome to DevDojo!"