Login Prompt Using Bash Script


Here's what it's gonna look like.


Pretty cool, huh?

So let's get started

First we need to know how to interact with the terminal's standard input and output. It's easier than you think actually.

So the two commands we need are the following:

1. Create a file called login.bash. And then copy and paste the following code:

            echo -n 'Enter your username: '
            read -a username
            echo -n 'Enter your password: '
            read -s password
            echo -e "Username: $username, Password: $password"

2. Hit save, and then run the script with the following command:

$ ./login.bash

The $ sign is to indicate that the above command is done inside a terminal window.

Code Breakdown

The code echo -n outputs the quoted text into the terminal without the new line \n.

read -a username will activate the standard input promt and will store the input value to the username placeholder. Also, the -a flag will only store the first word and not the whole line.

For password we use the -s flag when reading the std input to prevent the std out from printing the password.

And to get the value from a placeholder, prefix it with $. So in our case, it's $username and $password.

That's about it.