To read user input into your shell program using standard in, simply use the
printf 'How are you doing today? ' read user_input
The following will output the question "How are you doing today?" and allow the user to input some string.
You may use the
read command to read in multiple values at once, separated by a space.
printf 'Enter your last name, first name, and position: ' read last_name first_name position printf 'Your last name entered is %s. \n' $last_name printf 'Your first name entered is %s. \n' $first_name printf 'Your position is %s. \n' $position
We may hint at what info the user has to give by using the
printf 'What are your first and last names? ' read -p "First name: " first_name read -p "Last name: " last_name
This will output "First name: " on the command prompt, notifying the user that he or she needs to type out his first name.
Let's try a sample script named name that reads in a user's first name and last name, then outputs it back.
#!/bin/sh printf "Hello, welcome!\n" printf "What is your name? \n" read -p "First Name: " first_name read -p "Last Name: " last_name printf "Oh, okay %s %s. \nWelcome to the command line!" $first_name $last_name
Here's what it looks like in action:
Hello, welcome! What is your name? First Name: Ted Last Name: Bundy Oh, okay Ted Bundy. Welcome to the command line!
When a user inputs a password, we want to avoid having those characters echoed out. We can do this by turning off
#!/bin/bash printf "Enter a new password: " stty -echo # Turns off echoing characters read pass1 printf "\nPlease enter again: " read pass2 stty echo # Turn echoing feature back on printf "\nYou entered two passwords: %s and %s.\n" pass1 pass2
Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.$$ Check price
Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.$ Check price