Justin
Justin

Reputation: 4539

How do I read a value from user input into a variable

In ksh, how do I prompt a user to enter a value, and load that value into a variable within the script?

command line

echo Please enter your name: 

within the script

$myName = ?

Upvotes: 10

Views: 50010

Answers (3)

tripleee
tripleee

Reputation: 189618

ksh allows you to specify a prompt as part of the read command using this syntax:

read myName?"Please provide your name: "

Upvotes: 0

thebunnyrules
thebunnyrules

Reputation: 1670

You can do it in a single line, like so:

read -p "Please enter your name:" myName

To use variable in script

echo "The name you inputed is: $myName"
echo $myName

Upvotes: 8

Marcus Borkenhagen
Marcus Borkenhagen

Reputation: 6656

You want read:

echo Please enter your name:
read name
echo $name

See read(1) for more.

Upvotes: 20

Related Questions