Reading input (stdin)

Read what you type in the IDE's Input tab with read, one line or every line.

Runs in Zerethon IDE · Bash 5.2 Official Bash documentation ↗

A run cannot ask questions while it runs. Type the script's input in the Input tab before you press Run; read then reads it one line at a time.

main.sh
read -r name
read -r a b
echo "Hello, $name! $a + $b = $((a + b))"

Input (stdin)

Ada
3 4

Output

Hello, Ada! 3 + 4 = 7
main.sh
total=0
while IFS= read -r line || [[ -n $line ]]; do
  [[ -z $line ]] && continue
  (( total += line ))
done
echo "total: $total"

Input (stdin)

5
10
15

Output

total: 30
Reading every line of stdin until it ends.
  • read -r a b splits the line on spaces: the first word goes to a, the rest to b. -r keeps backslashes.
  • IFS= read -r line keeps the whole line, including leading spaces.
  • read fails at the end of the input. || [[ -n $line ]] still handles a last line that has no newline.
  • read -p "Name: " prompts are not shown here: there is no terminal, only the Input tab.