code-gijoe
code-gijoe

Reputation: 7234

How to input a read value when calling a shell script

Let say I have this line in a shell script in unix:

read -n 1

It will prompt the user to get value but is there a way to call the script so it takes the input as argument instead?

Like this for example:

myscript.sh "M"

I want to call the script from a build engine so it cannot answer with keyboard input.

Upvotes: 0

Views: 1266

Answers (4)

Lohit Gupta
Lohit Gupta

Reputation: 1081

To use argument values in shell script you can pass the argument with the script and then refer them using $1, $2, $3 and so on.

Upvotes: 2

user1934428
user1934428

Reputation: 22225

How about

myscript.sh $(read -n 1)

?

Upvotes: 0

anubhava
anubhava

Reputation: 784898

You can use Here Strings in bash:

myscript.sh <<< "M"

Or use pipeline:

echo "M" | myscript.sh

Here string documentation

Upvotes: 1

Gilbert
Gilbert

Reputation: 3776

Use this with some frequency:

hello() {  read -n1 CHAR;  echo $CHAR; }
echo world | hello

Upvotes: 0

Related Questions