Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to write a script where the initial command requires user input so that it prompts the user to enter that input before running?

Example:

sudo add-apt-repository ppa:"this"/"and_that"
share|improve this question
2  
Use this answer..no need for using echo ....read has -p option to prompt to user.. – heemayl Mar 7 at 6:43
up vote 4 down vote accepted

You can read user input in bash the following way:

read -p "Input this: " this
read -p "Input that: " that
sudo add-apt-repository ppa:${this}/${that}

read command creates a variable with its value taken from standard input.

In real life scenario you should also sanitize user input (i.e. check for non-alphanumeric characters before calling the command), but you can skip it if it's just for you.

share|improve this answer
    
The reference for read says: "The prompt is displayed only if input is coming from a terminal." I don't know what exactly that means. Is there some situation where prompt is not shown? – nobody Mar 7 at 6:40
    
For example if you pipe the input values to the script printf "this\nthat" | bash yourscript.sh – techraf Mar 7 at 6:46

You can use command read to read user input into variables.

#!/bin/bash

echo Enter this:
read this

echo and that:
read and_that

sudo add-apt-repository ppa:$this/$and_that
share|improve this answer
    
Why echo -ing? – heemayl Mar 7 at 6:37
    
I'll give that a try thank you. – Arturo Paz Mar 7 at 6:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.