The Linux Process Journey —” [” (Checking File Types and Comparing Values)

Shlomi Boutnaru, Ph.D.
2 min readJun 12, 2024

--

“[” is an ELF binary located at “/usr/bin/[“ — as shown in the screenshot below. It is an equivalent to the “test” utility (https://superuser.com/questions/334549/what-is-usr-bin-and-how-do-i-use-it). Thus, it is used for checking file types and comparing values (https://www.man7.org/linux/man-pages/man1/test.1.html). The binary is part of the “coreutils” package (https://github.com/coreutils/coreutils/blob/master/src/test.c#L36) — as shown in the screenshot below (taken from “copy.sh”, using the “Arch Linux”).

Overall, using “[“ we can check expressions, compare integers, check the type of a file (https://medium.com/@boutnaru/the-linux-concept-journey-linux-file-types-4cb622887331), check if a file has been modified since it was last read, if the file exists and the user has read/execute access and more (https://linux.die.net/man/1/test). We can retrieve the result of the check using “$?” — as shown in the screenshot below.

Lastly, in some shells like bash there is also an implementation of “[“ as a builtin command (https://linux.die.net/man/1/builtins) — as shown in the screenshot below. Also, “[“ is commonly used in scripts. For example, searching for it in scripts (using “grep.app”) yields more than 43,000 results (https://grep.app/search?q=%5B%20&filter[path][0]=scripts/).

See you in my next writeup ;-) You can follow me on twitter — @boutnaru (https://twitter.com/boutnaru). Also, you can read my other writeups on medium — https://medium.com/@boutnaru. You can find my free eBooks at https://TheLearningJourneyEbooks.com.

--

--