echo - Print Text

Write arguments to standard output.

Source: src/execution/builtins.f90:builtin_echo

Synopsis

echo [-neE] [arguments...]

Description

Outputs its arguments separated by spaces, followed by a newline. The echo builtin is POSIX-compliant by default: escape sequences are NOT interpreted unless -e is specified.

Options

OptionDescription
-nDo not print trailing newline
-eInterpret backslash escape sequences
-EDo not interpret escape sequences (default)

Options can be combined: -ne, -en, -nE

Escape Sequences (with -e)

SequenceCharacter
\aAlert (bell, ASCII 7)
\bBackspace (ASCII 8)
\cStop output (suppress newline and remaining text)
\fForm feed (ASCII 12)
\nNewline
\rCarriage return (ASCII 13)
\tHorizontal tab (ASCII 9)
\vVertical tab (ASCII 11)
\\Literal backslash

Return Value

CodeMeaning
0Success
1Write error

Examples

Basic output

echo Hello, World!
# Output: Hello, World!

No newline

echo -n "Enter name: "
read name

Escape sequences

echo -e "Line 1\nLine 2"
# Output:
# Line 1
# Line 2

echo -e "Column1\tColumn2"
# Output: Column1	Column2

echo -e "Bell: \a"  # System bell

Stop output

echo -e "This prints\c but this doesn't"
# Output: This prints

Combined options

echo -en "Loading...\r"
# Prints without newline, returns cursor to start

Implementation Details

From builtins.f90:

! Parse flags at start
do while (arg_index <= num_args)
    arg = args(arg_index)
    if (arg == "-n") then
        add_newline = .false.
    else if (arg == "-e") then
        interpret_escapes = .true.
    else if (arg == "-E") then
        interpret_escapes = .false.
    else if (arg == "-ne" .or. arg == "-en") then
        add_newline = .false.
        interpret_escapes = .true.

The escape sequence processor handles each sequence character-by-character:

case ('n')
    output(out_pos:out_pos) = char(10)  ! newline
case ('t')
    output(out_pos:out_pos) = char(9)   ! tab
case ('c')
    exit  ! stop processing

POSIX Compliance

fortsh's echo follows POSIX behavior:

  • Escape sequences are NOT interpreted by default
  • Use -e to enable escape interpretation
  • Some shells (like bash in non-POSIX mode) interpret escapes by default

For portable scripts, prefer printf which has consistent behavior across shells.

Related Commands

  • printf - Formatted output (more portable)
  • read - Read input

Notes

  • Empty arguments are preserved when quoted: echo "" "" outputs two empty strings
  • Arguments are separated by single spaces regardless of input whitespace
  • Uses file descriptor-aware I/O, respecting redirections