Tcl pwd Command
last modified April 3, 2025
The Tcl pwd
command returns the current working directory. It's a
fundamental command for file system navigation in Tcl scripts. The command is
particularly useful when working with file operations.
Basic Definition
The pwd
command stands for "print working directory". It returns
the absolute path of the current working directory as a string. This is the
directory where the Tcl script is currently operating.
Syntax: pwd
. The command takes no arguments and always returns the
full path to the current working directory. The path format is system-dependent.
Basic pwd Usage
This example demonstrates the simplest usage of the pwd
command.
set current_dir [pwd] puts "Current directory: $current_dir"
This captures the current working directory in a variable and prints it. The
pwd
command is called within square brackets for command substitution.
Comparing Directories
The pwd
output can be compared with other directory paths.
set home_dir "/home/user" if {[pwd] eq $home_dir} { puts "You're in your home directory" } else { puts "Current directory: [pwd]" }
This script compares the current directory with a predefined home directory. The
eq
operator performs string comparison. Different actions are taken
based on the comparison result.
Using pwd with File Operations
pwd
is often used with file operations to create absolute paths.
set filename "data.txt" set full_path [file join [pwd] $filename] puts "Full path to file: $full_path"
This combines the current directory with a filename using file join
.
The result is a platform-independent path to the file. This technique ensures
scripts work across different operating systems.
Changing and Restoring Directory
This example shows how to temporarily change directories while remembering the original location.
set original_dir [pwd] cd /tmp puts "Now in: [pwd]" cd $original_dir puts "Restored to: [pwd]"
The script stores the original directory, changes to /tmp, then returns. This pattern is useful when scripts need to operate in different directories. Always restore the original directory when done.
pwd in Procedures
The pwd
command can be used within procedures to get the current
directory at any point.
proc show_dir {} { puts "Procedure executing in: [pwd]" } puts "Script starts in: [pwd]" cd /var show_dir
This demonstrates that pwd
reflects the current directory at call
time, not definition time. The procedure shows the directory where it's called,
which may differ from where it was defined.
Error Handling with pwd
While pwd
rarely fails, it's good practice to handle potential errors.
if {[catch {set current_dir [pwd]} err]} { puts "Error getting current directory: $err" } else { puts "Current directory: $current_dir" }
This uses catch
to handle any potential errors from pwd
.
While unlikely, directory access might fail due to permissions or other system
issues. Robust scripts should account for such possibilities.
Best Practices
- Absolute Paths: Use pwd to create absolute paths for reliability.
- Directory Changes: Always store original directory when changing.
- Error Handling: Consider wrapping pwd in catch for robustness.
- Platform Independence: Use file join with pwd for cross-platform paths.
- Logging: Log current directory in scripts for debugging.
This tutorial covered the Tcl pwd
command with practical examples
showing its usage in different scenarios. The command is essential for file
system navigation in Tcl scripts.
Author
List all Tcl Tutorials.