{"id":68155,"date":"2022-10-26T09:00:49","date_gmt":"2022-10-26T09:00:49","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=68155"},"modified":"2022-10-26T09:00:49","modified_gmt":"2022-10-26T09:00:49","slug":"tips-for-using-the-linux-test-command","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=68155","title":{"rendered":"Tips for using the Linux test command"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Tips for using the Linux test command<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">Seth Kenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Wed, 10\/26\/2022 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>The <code>[<\/code> command, often called a &#8220;test,&#8221; is a command from the GNU Core Utils package, and initiates a conditional statement in Bash. Its function is exactly the same as the <code>test<\/code> command. When you want to execute a command only when something is either true or false, use the <code>[<\/code> or the <code>test<\/code> command. However, there&#8217;s a significant difference between <code>[<\/code> or <code>test<\/code> and <code>[[<\/code>, and there&#8217;s a technical difference between those commands and your shell&#8217;s versions of them.<\/p>\n<h2>[ vs test commands in Linux<\/h2>\n<p>The <code>[<\/code> and the <code>test<\/code> commands, installed by the GNU Core Utils package, perform the same function using a slightly different syntax. (You might find it difficult to search for documentation using the single left-square bracket character, however, so many users find <code>test<\/code> easier to reference.) Bash and similar shells happen to <em>also<\/em> have the <code>[<\/code> and the <code>test<\/code> commands built-in, and the built-in versions supersede the ones installed in <code>\/usr\/bin<\/code>. In other words, when you use <code>[<\/code> or <code>test<\/code>, you&#8217;re probably not executing <code>\/usr\/bin\/[<\/code> or <code>\/usr\/bin\/test<\/code>. Instead, you&#8217;re invoking what&#8217;s essentially a function of your Bash shell.<\/p>\n<p>You might wonder why <code>[<\/code> or <code>test<\/code> exist in <code>\/usr\/bin<\/code> at all. Some shells, such as <a href=\"https:\/\/opensource.com\/article\/20\/8\/tcsh\">tcsh<\/a>, don&#8217;t have <code>[<\/code> and <code>test<\/code> built-in, so if you want to use those commands in that shell, you must have them installed as separate binaries.<\/p>\n<p>The bottom line is that as long as you don&#8217;t get an error when you type a command starting with <code>[<\/code> or <code>test<\/code>, then you&#8217;ve got everything you need. It almost never matters whether your shell or your <code>bin<\/code> directory is providing the commands.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More Linux resources<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/linux-commands-cheat-sheet\/?intcmp=70160000000h1jYAAQ\">Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/advanced-linux-commands\/?intcmp=70160000000h1jYAAQ\">Advanced Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/rh024-red-hat-linux-technical-overview?intcmp=70160000000h1jYAAQ\">Free online course: RHEL technical overview<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-networking?intcmp=70160000000h1jYAAQ\">Linux networking cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-selinux?intcmp=70160000000h1jYAAQ\">SELinux cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/linux-common-commands-cheat-sheet?intcmp=70160000000h1jYAAQ\">Linux common commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources\/what-are-linux-containers?intcmp=70160000000h1jYAAQ\">What are Linux containers?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux?intcmp=70160000000h1jYAAQ\">Our latest Linux articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Testing for a file<\/h2>\n<p>It&#8217;s common to want to know whether a file exists, often so you can confidently proceed with some action, or so you can avoid &#8220;clobbering&#8221; it with a file of the same name. In an interactive shell session, you can just look to see whether the file exists but in a shell script, you need the computer to determine that for itself. The <code>-e<\/code> option tests whether a file exists, but its apparent response is the same either way.<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> example<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> example<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> notafile<br>\n$<\/div><\/div><\/pre>\n<p>The <code>[<\/code> and <code>test<\/code> commands are essentially switches. They emit a <code>true<\/code> or <code>false<\/code> response, but considers both of them as success. You can put this to use by pairing the commands with logical operators, such as <code>&amp;&amp;<\/code> and <code>||<\/code>. The <code>&amp;&amp;<\/code> operator is executed when a response is <code>true<\/code>:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> example<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> example <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"foo\"<\/span><br>\nfoo<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> notafile <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"foo\"<\/span><br>\n$<\/div><\/div><\/pre>\n<p>The <code>||<\/code> operator executes when a response is <code>false<\/code>:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> example<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> example <span class=\"sy0\">||<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"foo\"<\/span><br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> notafile <span class=\"sy0\">||<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"foo\"<\/span><br>\nfoo<br>\n$<\/div><\/div><\/pre>\n<p>If you prefer, you can use square brackets instead of <code>test<\/code>. In all cases, the results are the same:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> example<br>\n$ <span class=\"br0\">[<\/span> <span class=\"re5\">-e<\/span> example <span class=\"sy0\">&amp;<\/span><span class=\"co0\">#93; &amp;&amp; echo \"foo\"<\/span><br>\nfoo<br>\n$ <span class=\"br0\">[<\/span> <span class=\"re5\">-e<\/span> notafile <span class=\"sy0\">&amp;<\/span><span class=\"co0\">#93; &amp;&amp; echo \"foo\"<\/span><br>\n$<\/div><\/div><\/pre>\n<h2>Testing for file types<\/h2>\n<p>Everything in Linux is a file, so when you can test for the existence of a directory with the <code>-e<\/code> option, the same way you test for a file. However, there are different kinds of files, and sometimes that matters. You can use <code>[<\/code> or <code>test<\/code> to detect a variety of different file types:<\/p>\n<ul>\n<li>\n<p><code>-f<\/code>: regular file (returns <code>false<\/code> for a directory)<\/p>\n<\/li>\n<li>\n<p><code>-d<\/code>: directory<\/p>\n<\/li>\n<li>\n<p><code>-b<\/code>: block (such as <code>\/dev\/sda1<\/code>)<\/p>\n<\/li>\n<li>\n<p><code>-L<\/code> or <code>-h<\/code>: symlink<\/p>\n<\/li>\n<li>\n<p><code>-S<\/code>: socket<\/p>\n<\/li>\n<\/ul>\n<p>There are more, but those tend to be the most common.<\/p>\n<h2>Testing for file attributes<\/h2>\n<p>You can also look at metadata of a file:<\/p>\n<ul>\n<li>\n<p><code>-s<\/code>: a file with the size greater than zero<\/p>\n<\/li>\n<li>\n<p><code>-N<\/code>: a file that&#8217;s been modified since it was last read<\/p>\n<\/li>\n<\/ul>\n<p>You can test by ownership:<\/p>\n<ul>\n<li>\n<p><code>-O<\/code>: a file owned by the current primary user<\/p>\n<\/li>\n<li>\n<p><code>-G<\/code>: a file owned by the current primary group<\/p>\n<\/li>\n<\/ul>\n<p>Or you can test by permissions (or <em>file mode<\/em>):<\/p>\n<ul>\n<li>\n<p><code>-r<\/code>: a file with read permission granted<\/p>\n<\/li>\n<li>\n<p><code>-w<\/code>: a file with write permission granted<\/p>\n<\/li>\n<li>\n<p><code>-x<\/code>: a file with execute permission granted<\/p>\n<\/li>\n<li>\n<p><code>-k<\/code>: a file with the sticky bit set<\/p>\n<\/li>\n<\/ul>\n<h2>Combining tests<\/h2>\n<p>You don&#8217;t always just have to test for a single attribute. The <code>-a<\/code> option (&#8220;and&#8221;) allows you to string several tests together, with the requirement that all tests return as <code>true<\/code>:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> zombie apocalypse now<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> zombie <span class=\"re5\">-a<\/span> <span class=\"re5\">-e<\/span> apocalypse <span class=\"re5\">-a<\/span> <span class=\"re5\">-e<\/span> now <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"no thanks\"<\/span><br>\nno thanks<\/div><\/div><\/pre>\n<p>If any expression fails, then the test returns <code>false<\/code>:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> zombie apocalypse now<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> plant <span class=\"re5\">-a<\/span> <span class=\"re5\">-e<\/span> apocalypse <span class=\"re5\">-a<\/span> <span class=\"re5\">-e<\/span> now <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"no thanks\"<\/span><br>\n$<\/div><\/div><\/pre>\n<p>The <code>-o<\/code> option (&#8220;or&#8221;) requires that one expression is true:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> zombie apocalypse now<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re5\">-e<\/span> zombie <span class=\"re5\">-o<\/span> <span class=\"re5\">-e<\/span> plant <span class=\"re5\">-o<\/span> <span class=\"re5\">-e<\/span> apocalypse <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"no thanks\"<\/span><br>\nno thanks<\/div><\/div><\/pre>\n<h2>Integer tests<\/h2>\n<p>You can also test integers. That&#8217;s not necessarily directly useful (you probably inherently know that 0 is less than 1, for instance) but it&#8217;s invaluable when you&#8217;re using variables in a script.<\/p>\n<p>The operators are fairly intuitive once you understand the schema:<\/p>\n<ul>\n<li>\n<p><code>-eq<\/code>: equal to<\/p>\n<\/li>\n<li>\n<p><code>-ne<\/code>: not equal<\/p>\n<\/li>\n<li>\n<p><code>-ge<\/code>: greater than or equal to<\/p>\n<\/li>\n<li>\n<p><code>-gt<\/code>: greater than<\/p>\n<\/li>\n<li>\n<p><code>-le<\/code>: less than or equal to<\/p>\n<\/li>\n<li>\n<p><code>-lt<\/code>: less than<\/p>\n<\/li>\n<\/ul>\n<p>Here&#8217;s a simple example:<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"re2\">nil<\/span>=<span class=\"nu0\">0<\/span><br>\n$ <span class=\"re2\">foo<\/span>=<span class=\"nu0\">1<\/span><br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re1\">$foo<\/span> <span class=\"re5\">-eq<\/span> <span class=\"re1\">$nil<\/span> <span class=\"sy0\">||<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"Those are not equal.\"<\/span><br>\nThose are not equal.<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re1\">$foo<\/span> <span class=\"re5\">-eq<\/span> <span class=\"nu0\">1<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"Those are equal.\"<\/span><\/div><\/div><\/pre>\n<p>Of course, you can combine tests.<\/p>\n<pre class=\"highlight\">\n<div class=\"geshifilter\"><div class=\"bash geshifilter-bash\">$ <span class=\"kw2\">touch<\/span> example<br>\n$ <span class=\"kw3\">test<\/span> <span class=\"re1\">$foo<\/span> <span class=\"re5\">-ne<\/span> <span class=\"re1\">$nil<\/span> <span class=\"re5\">-a<\/span> <span class=\"re5\">-e<\/span> example <span class=\"re5\">-o<\/span> <span class=\"re5\">-e<\/span> notafile <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">echo<\/span> <span class=\"st0\">\"yes\"<\/span><br><span class=\"kw2\">yes<\/span><\/div><\/div><\/pre>\n<h2>Testing testing<\/h2>\n<p>The <code>[<\/code> and <code>test<\/code> commands are vital conditional statements when scripting. These are easy and common ways to control the flow of your code. There are yet more tests available than what I&#8217;ve covered in this article, so whether you used Bash, tcsh, ksh, or some other shell entirely, take a look at the man page to get the full spectrum of what these commands offer.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>The [ and test commands are vital conditional statements when scripting.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/10\/command_line_prompt.png\" width=\"518\" height=\"292\" alt=\"Command line prompt\" title=\"Command line prompt\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Opensource.com<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux\" hreflang=\"en\">Linux<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/command-line\" hreflang=\"en\">Command line<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/10\/cc-by-sa--28.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tips for using the Linux test command Seth Kenlon Wed, 10\/26\/2022 &#8211; 03:00 The [ command, often called a &#8220;test,&#8221; is a command from the GNU Core Utils package, and initiates a conditional statement in Bash. Its function is exactly the same as the test command. When you want to execute a command only when [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":68156,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-68155","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68155","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68155"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68155\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/68156"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}