Skip to content

Latest commit

 

History

History
43 lines (32 loc) · 1.09 KB

08_read_user_input.md

File metadata and controls

43 lines (32 loc) · 1.09 KB

Reading User Input

Shell Script Arguments

Let’s make our script more dynamic by consuming a command line argument when we run the script.

Command line arguments can be referenced using $# or ${##}, based on position in the command line.

First parameter would be $1, second parameter $2, 10th parameter would be ${10}.

Let’s edit the hello_world.sh file using vim to use a command line argument:

#!/bin/bash
echo "Hello World, $1 $2!"

Try this now ex. 3:

BabyMac:~ jennapederson$ ./hello_world.sh Jenna Pederson
Hello World, Jenna Pederson!

Reading Interactively From The User

You can read input from a user interactively.

Let’s edit the hello_world.sh file using vim to prompt and read input from the user:

#!/bin/bash
echo -n "What is your name? "
read name
echo "Hello World, $name!"

Try this now ex. 4:

BabyMac:~ jennapederson$ ./hello_world.sh
What is your name? Jenna Pederson
Hello World, Jenna Pederson!

Back: Permissions Forward: Connecting Things Together