Taking User Input.

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

  1. Create a New File:

    sh

    vi example_script.bash
    
  2. 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!"
    
  3. Save the File:

    • In vi or vim: Press Esc to exit insert mode, then type :wq to save and exit.

    • In nano: Press Ctrl + O to write out and then Ctrl + X to exit.

  4. Make the Script Executable:

    sh

    chmod +x example_script.bash
    
  5. Run the Script:

    sh

    ./example_script.bash
    

Explanation of the Script

And that's it! You've created a simple Bash script that takes user input and responds with a greeting