BASENAME(3P)
β¨π BASENAME(3P) πβ¨
welcome to the POSIX Programmer's Manual, bestie β this is the page for basename, straight out of the POSIX programmer's manual gang. come get your last path component, it's cute, it's free, it's giving "I know where I live" energy. π¦
πΈ PROLOG πΈ
okay so real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so likeβ¦ consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux. no cap, check before you ship it. π
π NAME π
basename β return the last component of a pathname. that's it. that's the whole job. and she ate. π
π¦ SYNOPSIS π¦
#include <libgen.h>
char *basename(char *path);
π DESCRIPTION π
so the basename() function shall take the pathname pointed to by path and return a pointer to the final component of the pathname, deleting any trailing '/' characters. like it's doing a little haircut on your slashes. βοΈ
If the string pointed to by path consists entirely of the '/' character, basename() shall return a pointer to the string "/". And if the string pointed to by path is exactly "//", it is implementation-defined whether '/' or "//" is returned β so like, don't build your whole personality on that one, it depends on the machine. π
If path is a null pointer or points to an empty string, basename() shall return a pointer to the string ".".
β οΈ bestie, listen: thebasename()function may modify the string pointed to bypath, and may return a pointer to internal storage. The returned pointer might be invalidated or the storage might be overwritten by a subsequent call tobasename(). The returned pointer might also be invalidated if the calling thread is terminated. so please, for the love of everything, do not keep that pointer around like a souvenir. π
The basename() function need not be thread-safe. ngl that's just how she is.
π RETURN VALUE π
The basename() function shall return a pointer to the final component of path. slay, that's the whole return value, nothing extra. π«Ά
π ERRORS π
No errors are defined. iconic, no drama, no errno nonsense. β¨
The following sections are informative. (iykyk β that means they're the vibe-and-context sections, not the normy requirements ones. π)
π EXAMPLES π
πΈ Using basename() πΈ
The following program fragment returns a pointer to the value lib, which is the base name of /usr/lib.
#include <libgen.h>
...
char name[] = "/usr/lib";
char *base;
base = basename(name);
...
see how she justβ¦ finds the lib at the end? main character energy fr fr. π
π Sample Input and Output Strings for the basename() and dirname() Functions and the basename and dirname Utilities π
okay so this is the big glow-up chart. the columns in the original are, in order: basename() and dirname() Functions path Argument, String Returned by basename(), String Returned by dirname(), basename and dirname Utilities string Operand, Output Written by basename Utility, and Output Written by dirname Utility. here's every single row, no skipping: π
- path
"usr"βbasename()returns"usr",dirname()returns"."; utility operandusrβbasenameutility writesusr,dirnameutility writes. - path
"usr/"βbasename()returns"usr",dirname()returns"."; utility operandusr/βbasenameutility writesusr,dirnameutility writes. - path
""βbasename()returns".",dirname()returns"."; utility operand""βbasenameutility writes. or empty string,dirnameutility writes. - path
"/"βbasename()returns"/",dirname()returns"/"; utility operand/βbasenameutility writes/,dirnameutility writes/ - path
"//"βbasename()returns"/" or "//",dirname()returns"/" or "//"; utility operand//βbasenameutility writes/ or //,dirnameutility writes/ or // - path
"///"βbasename()returns"/",dirname()returns"/"; utility operand///βbasenameutility writes/,dirnameutility writes/ - path
"/usr/"βbasename()returns"usr",dirname()returns"/"; utility operand/usr/βbasenameutility writesusr,dirnameutility writes/ - path
"/usr/lib"βbasename()returns"lib",dirname()returns"/usr"; utility operand/usr/libβbasenameutility writeslib,dirnameutility writes/usr - path
"//usr//lib//"βbasename()returns"lib",dirname()returns"//usr"; utility operand//usr//lib//βbasenameutility writeslib,dirnameutility writes//usr - path
"/home//dwc//test"βbasename()returns"test",dirname()returns"/home//dwc"; utility operand/home//dwc//testβbasenameutility writestest,dirnameutility writes/home//dwc
every row accounted for, every slash where it belongs. obsessed. π
π§ APPLICATION USAGE π§
None. short queen. nothing to add. π«Ά
β RATIONALE β
None. again, no notes, she's confident. π
π« FUTURE DIRECTIONS π«
None. she's not going anywhere, she's booked and busy as-is. β¨
π₯ SEE ALSO π₯
dirname()- The Base Definitions volume of POSIX.1β2017,
<libgen.h> - The Shell and Utilities volume of POSIX.1β2017,
basename
go read your sister pages, bestie, they're literally right there. π
π COPYRIGHT π
okay legal girlies, here's the fine print, and yes it all has to stay: Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
π and that's the whole page! courtesy of IEEE/The Open Group, dated 2017, filed under BASENAME(3P). mwah. stay slaying, and remember: delete your trailing slashes, not your standards. π β¨π¦
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms