0

Apologies if this is a basic concept, but I was wondering how to incorporate the userinput immediately after running the code. Same line, no prompt.

I tried:

 #!/bin/bash 
 read userinput

but that results in reading the userinput in the next line over. i.e. (assuming UserInput is 2):

$./programname
2

For reference, I want to essentially type:

$./programname 2

directly into the command line, without a newline.

D.I.
  • 31
  • 6
  • Such input is passed as **arguments**, not input on stdin. See `"$1"`, `"$2"`, etc. [BashFAQ #35](http://mywiki.wooledge.org/BashFAQ/035) is also pertinent. – Charles Duffy May 22 '17 at 20:06
  • An important note, by the way -- the literal string typed by the user is parsed into a list of arguments *by the calling shell*, not the shell that's invoked to run your script. That means that by the time the shell that actually runs your script has started, the literal text that was typed the user no longer exists, and all you have is an array of strings -- so you can't tell if the user typed `"hello world"` or `hello\ world`; or tell the difference between a user who typed `*.txt` and one who typed `a.txt b.txt`. – Charles Duffy May 22 '17 at 20:08
  • @M.Becerra, you mean `userinput=$1`. And it needs to be `echo "$userinput"`, **with the quotes**, to not fall into [BashPitfalls #14](http://mywiki.wooledge.org/BashPitfalls#echo_.24foo). – Charles Duffy May 22 '17 at 20:09

0 Answers0