Tcl puts Command
last modified April 3, 2025
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 quotes around the string are required for multi-word strings.
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.
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 the string.
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.
Best Practices
- Newlines: Use
-nonewlineonly when needed. - Files: Always close files after writing.
- Buffering: Use
flushfor immediate output. - Errors: Check file operations for success.
- 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.