Tcl puts Command
last modified October 20, 2026
The Tcl puts command is used to output text to the console or files.
It's the primary command for displaying information in Tcl scripts. The command
is simple yet versatile.
Basic Definition
The puts command writes strings to the standard output by default.
It can also write to files when a file channel is specified. The command
automatically adds a newline unless instructed otherwise.
Syntax: puts ?-nonewline? ?channelId? string. The optional
-nonewline suppresses the newline. channelId specifies
an output channel.
Basic Output
This example demonstrates the simplest usage of puts to display text.
puts "Hello, Tcl World!"
This command outputs the string "Hello, Tcl World!" followed by a newline.
The string must be quoted with double quotes or braces when it contains spaces;
otherwise puts receives too many arguments and raises an error.
Output Without Newline
The -nonewline option prevents puts from adding a newline.
puts -nonewline "Enter your name: " gets stdin name puts "Hello, $name"
This creates a prompt that waits for user input on the same line. The gets
command reads input, and the final puts displays a greeting. The
prompt has no newline, so on a buffered stdout it may only appear after the
input unless you call flush stdout. Here standard input is empty,
so name is empty and the greeting ends after the comma.
Output to a File
puts can write to files by specifying a file channel.
set file [open "output.txt" w] puts $file "This goes to a file" close $file
This opens a file for writing, writes a string to it, then closes the file.
The w mode truncates existing files or creates new ones.
Output Variables
puts can display variable values using substitution.
set language "Tcl" set version 8.6 puts "Using $language version $version"
This shows how to output variable values within a string. The dollar sign
triggers variable substitution in a double-quoted string; inside braces,
puts {Using $language} would print the text unchanged.
Formatted Output
puts can display formatted output using the format command.
set pi 3.14159265359 puts [format "Pi rounded to 2 decimals: %.2f" $pi]
This demonstrates formatting a floating-point number to 2 decimal places.
The format command provides C-style formatting capabilities.
Multi-line Output
puts can output multiple lines using newline characters.
puts "Line 1\nLine 2\nLine 3"
This shows how to create multi-line output with a single puts command.
The \n escape sequence represents a newline character. It is
replaced inside double quotes; a string in braces keeps the backslash and the
n as they are.
Best Practices
- Newlines: Use
-nonewlineonly when needed. - Files: Always close files after writing.
- Buffering: Use
flushfor immediate output. - Errors: Check file operations for success, and write diagnostics to
stderrwithputs stderr. - Formatting: Use
formatfor complex output.
This tutorial covered the Tcl puts command with practical
examples showing its usage in different scenarios.
Author
List all Tcl Tutorials.