Sh - Standard input (stdin)

> Procedural Languages > Bash Shell and (Unix|Linux) Utilities (XCU)

1 - About

Standard input in the shell.

Advertising

3 - Management

3.1 - Start a standard input session

cat will start a standard input session

cat

You can redirect it to a file.

cat > myinput.txt

3.2 - Stop

Ctrl+D typed at the start of a line on a terminal, signifies the end of the file (input).

Example:

cat -

Type what you want then Ctrl+D

3.3 - Redirect

3.3.1 - Standard

You can automate the input with the input redirection operator

  • Create the following file: myScript.sh
read -p "Type Something: " myVar
echo "You answer was :" $myVar
read -p "Type still Something: " myVar
echo "Your second answer was :" $myVar
  • Create an input text file with two lines Hello and World
 echo $'Hello\nWorld' > myAnswer.txt
  • Give it to your script
./myScript.sh < myAnswer.txt
  • Output:
You answer was : Hello
Your second answer was : World
Advertising

3.3.2 - Echo with pipe

If your command needs plain input without terminal emulation you can use input redirection like:

echo "some input" | command

3.3.3 - Here document

Example for an Here_document (a input document in the code) with Sh - tr (translate or delete characters) and an output redirection.

tr a-z A-Z << END_TEXT > myText.file
 one two three
 four five six
 END_TEXT
ONE TWO THREE
FOUR FIVE SIX

See also: http://tldp.org/LDP/abs/html/here-docs.html