Bash - Standard input (stdin)

1 - About

3 - Creation

3.1 - Manual

3.2 - 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

3.3 - 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

3.4 - 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


Data Science
Data Analysis
Statistics
Data Science
Linear Algebra Mathematics
Trigonometry

Powered by ComboStrap