In this Article we gonna see how to take user input from keyboard. So, Let's focus on taking user input in a Bash script and use the .bash extension. Here are the steps:
Creating and Running a Bash Script with User Input
Create a New File:
sh
vi example_script.bash
Write Your Script: Here's a simple example that takes user input and greets the user.
bash
#!/bin/bash echo "Enter your name:" read name echo "Hello, $name!"
Save the File:
In
viorvim: PressEscto exit insert mode, then type:wqto save and exit.In
nano: PressCtrl + Oto write out and thenCtrl + Xto exit.
Make the Script Executable:
sh
chmod +x example_script.bash
Run the Script:
sh
./example_script.bash
Explanation of the Script
#!/bin/bash: This is the shebang line, indicating that the script should be executed using the Bash shell.echo "Enter your name:": This command prints the prompt asking the user to enter their name.read name: This command takes the user input and stores it in the variablename.echo "Hello, $name!": This command prints a greeting using the value stored in thenamevariable.
And that's it! You've created a simple Bash script that takes user input and responds with a greeting