Bash - Standard input (stdin)

About

Creation

Manual

File

The input redirection operator takes the content of a file and send it as standard input

Example with read that take a stand

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

Inline string

The input redirection operator «< takes a string and send it as standard input

Example with read that take a stand

read -p "you name: " name <<< "Nico"
echo $name
  • Output:
Nico

Here document

Example for an Here document (a input document in the code) with tr 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

Powered by ComboStrap