filename(3tcl)
πΈ NAME πΈ
filename β file name conventions supported by Tcl commands. Like, the whole tea on how Tcl thinks about paths, bestie. (This page lives in the Tcl Built-In Commands manual, iykyk.)
β¨ INTRODUCTION β¨
Okay so, listen. Every single Tcl command and C procedure that takes file names as arguments expects those file names to be in one of three forms, depending on the current platform. On each platform, Tcl supports file names in the standard form(s) for that platform.
And on all platforms β yes, all of them β Tcl also supports a Unix-like syntax, intended to give you a convenient way of constructing simple file names. Cute, right?
But here's the thing, and I need you to hear me on this one: scripts that are intended to be portable should NOT assume a particular form for file names. Instead, portable scripts must use the file split and file join commands to manipulate file names (peep the file manual entry for more deets). That's the move. Periodt.
π¦ PATH TYPES π¦
File names get sorted into three general types based on where the path starts β the starting point used to specify the file:
- Absolute β completely qualified. Gives a path to the file relative to a particular volume and the root directory on that volume.
- Relative β unqualified. Gives a path to the file relative to the current working directory.
- Volume-relative β partially qualified. Either gives the path relative to the root directory on the current volume, or relative to the current directory of the specified volume.
And if you ever need to know which one you're holding? The file pathtype command can be used to determine the type of a given path. Slay. π₯
π PATH SYNTAX π
The rules for native names depend on the value reported in the platform element of the tcl_platform array. Let's break it down by vibe:
π§ Unix
On Unix and Apple macOS platforms, Tcl uses path names where the components are separated by slashes. Path names may be relative or absolute, and file names may contain any character other than slash. The file names . and .. are special and refer to the current directory and the parent of the current directory respectively.
Multiple adjacent slash characters are interpreted as a single separator β except for the first double slash // in absolute paths. And any number of trailing slash characters at the end of a path are simply ignored, so the paths foo, foo/ and foo// are all identical. In particular, foo/ does not necessarily mean a directory is being referred. π
The following examples illustrate various forms of path names:
/β Absolute path to the root directory./etc/passwdβ Absolute path to the file namedpasswdin the directoryetcin the root directory..β Relative path to the current directory.fooβ Relative path to the filefooin the current directory.foo/barβ Relative path to the filebarin the directoryfooin the current directory.../fooβ Relative path to the filefooin the directory above the current directory.
π Windows
On Microsoft Windows platforms, Tcl supports both drive-relative and UNC style names. Both / and \ may be used as directory separators in either type of name.
Drive-relative names consist of an optional drive specifier followed by an absolute or relative path.
UNC paths follow the general form \\servername\sharename\path\file, but must at the very least contain the server and share components, i.e. \\servername\sharename.
In both forms, the file names . and .. are special and refer to the current directory and the parent of the current directory respectively.
The following examples illustrate various forms of path names:
\\Host\share/fileβ Absolute UNC path to a file calledfilein the root directory of the export pointshareon the hostHost. Note that repeated use offile dirnameon this path will give//Host/share, and will never give just//Host.c:fooβ Volume-relative path to a filefooin the current directory on drivec.c:/fooβ Absolute path to a filefooin the root directory of drivec.foo\barβ Relative path to a filebarin thefoodirectory in the current directory on the current volume.\fooβ Volume-relative path to a filefooin the root directory of the current volume.\\fooβ Volume-relative path to a filefooin the root directory of the current volume. This is not a valid UNC path, so the assumption is that the extra backslashes are superfluous.
π Zipfs
On all platforms where zipfs support is enabled, paths within mounted ZIP archives begin with the string returned by the zipfs root command. Zipfs paths are case-sensitive on all platforms. π
π TILDE SUBSTITUTION π
Unlike earlier versions of Tcl, Tcl 9 does not do implicit tilde substitution on file paths β with the exception noted below, bestie. The commands file home and file tildeexpand may be used to explicitly accomplish the same.
So the exception: initialization of theauto_pathvariable and the Tcl module search paths, as documented in the manpages fortclvarsandtm.
When any path in an environment variable used to initialize these starts with a tilde, it will be interpreted as if the first element is replaced with the location of the home directory for the given user. If the tilde is followed immediately by a separator, the $HOME environment variable is substituted. Otherwise the characters between the tilde and the next separator are taken as a user name, which is used to retrieve the user's home directory for substitution. This works on POSIX, macOS and Windows platforms. π«Ά
β PORTABILITY ISSUES β
Not all file systems are case sensitive, so scripts should avoid code that depends on the case of characters in a file name. In addition, the character sets allowed on different devices may differ, so scripts should choose file names that do not contain special characters like:
<>:?"/\|
The safest approach is to use names consisting of alphanumeric characters only. Care should be taken with filenames which contain spaces (common on Windows systems) and filenames where the backslash is the directory separator (Windows native path names).
On Windows platforms there are file and path length restrictions. Complete paths or filenames longer than about 260 characters will lead to errors in most file operations. No cap, that's just how it is. π
Another Windows peculiarity is that any number of trailing dots β.β in filenames are totally ignored, so, for example, attempts to create a file or directory with a name βfoo.β will result in the creation of a file/directory with name βfooβ. This fact is reflected in the results of file normalize. Furthermore, a file name consisting only of dots β.........β or dots with trailing characters β.....abcβ is illegal. π
π SEE ALSO π
π₯ KEYWORDS π₯
current directory, absolute file name, relative file name, volume-relative file name, portability
okay that's the whole page bestie, you ate π This one's from the Tcl Built-In Commands manual, Tcl version 7.5, and it's filed under filename(3tcl) β mwah, go forth and never hardcode a path separator again πβ¨
π¦ where this page comes from
- debian trixie: tcl9.0-doc 9.0.1+dfsg-2
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 1995-1996 Sun Microsystems, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms