{"id":3885,"date":"2018-05-15T21:16:31","date_gmt":"2018-05-15T17:16:31","guid":{"rendered":"https:\/\/www.howtoforge.com\/linux-test-command\/"},"modified":"2018-05-15T21:16:31","modified_gmt":"2018-05-15T17:16:31","slug":"linux-test-command-tutorial-for-beginners-with-examples","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/linux-test-command-tutorial-for-beginners-with-examples\/","title":{"rendered":"Linux test Command Tutorial for Beginners (with Examples)"},"content":{"rendered":"<p>Sometimes, while working on the Linux command line, you might want to test certain things like integer values, or whether or not a file is of certain type? You&#8217;ll be glad to know there&#8217;s a built-in command line utility <strong>test<\/strong> that lets you do most of these comparisons and tests.<\/p>\n<p>In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it&#8217;s worth mentioning that all examples in the article have been tested on Ubuntu 16.04 LTS.<\/p>\n<h2 id=\"linux-test-command\">Linux test command<\/h2>\n<p>As already mentioned above, the test command is used to perform checks and comparisons. Here&#8217;s its syntax:<\/p>\n<pre>test EXPRESSION<\/pre>\n<p>And here&#8217;s what the man page says about this utility:<\/p>\n<pre>test - check file types and compare values<\/pre>\n<p>Following are some Q&amp;A-styled examples that should give you a good idea on how the tool works.<\/p>\n<p><strong>Note<\/strong>: Keep in mind that some of the command line options test provides are most useful when used within shell scripts.<\/p>\n<h2 id=\"q-how-to-compare-two-strings\">Q1. How to compare two strings?<\/h2>\n<p>Simple, you just need to use the equal to (=) sign between them. For example:<\/p>\n<p class=\"command\">test howto = forge<\/p>\n<p>A better way would be to write something like this:<\/p>\n<p class=\"command\">test howto = forge &amp;&amp; echo &#8220;same&#8221;<\/p>\n<p>So if the strings are same, the word &#8220;same&#8221; should be printed in output, else nothing should be printed.<\/p>\n<p><a class=\"fancybox\" id=\"img-test-str-comparison\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/test-str-comparison.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples.png\" alt=\"How to compare two strings?\" width=\"453\" height=\"51\" title=\"\"><\/a><\/p>\n<p>Similarly, you can use the following template if you want to test for inequality.<\/p>\n<pre>STRING1 != STRING2<\/pre>\n<h2 id=\"q-how-to-compare-integers-using-test\">Q2. How to compare integers using test?<\/h2>\n<p>This is also very straight forward &#8211; just compare them using &#8216;-eq&#8217;. For example:<\/p>\n<p class=\"command\">test 5 -eq 7 &amp;&amp; echo &#8220;same&#8221;<\/p>\n<p>Here&#8217;s a screenshot showing how this command line option works:<\/p>\n<p><a class=\"fancybox\" id=\"img-test-int-comp\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/test-int-comp.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-1.png\" alt=\"How to compare integers using test?\" width=\"397\" height=\"51\" title=\"\"><\/a><\/p>\n<p>Similarly, you can use &#8216;<strong>-ge<\/strong>&#8216; to test greater than or equal to, &#8216;<strong>-gt<\/strong>&#8216; for greater than, &#8216;<strong>-le<\/strong>&#8216; for less than or equal to, &#8216;<strong>-lt<\/strong>&#8216; for less than, and &#8216;<strong>-ne<\/strong>&#8216; for not equal.<\/p>\n<h2 id=\"q-how-to-testcompare-files-using-test\">Q3. How to test\/compare files using test?<\/h2>\n<p>To test which of the two files are newer, use &#8216;-nt&#8217;. For example:<\/p>\n<p class=\"command\">test file1 -nt file2<\/p>\n<p>Here&#8217;s how I tested it on my system:<\/p>\n<p><a class=\"fancybox\" id=\"img-test-file-new\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/test-file-new.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-2.png\" alt=\"How to test\/compare files using test?\" width=\"478\" height=\"36\" title=\"\"><\/a><\/p>\n<p>Other file comparisons that you can perform include which among the two files is older (-ot) and whether two files have same device and inode numbers (-ef).<\/p>\n<p>To check whether a given file is a directory, use the -d option in the following way:<\/p>\n<p class=\"command\">test -d [filename]\n<p>For example:<\/p>\n<p class=\"command\">test -d new_dir<\/p>\n<p>Following are some other file-type test options the &#8216;test&#8217; command offers:<\/p>\n<p><a class=\"fancybox\" id=\"img-test-file-types\" href=\"https:\/\/www.howtoforge.com\/images\/command-tutorial\/big\/test-file-types.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-3.png\" alt=\"Result of test command use\" width=\"440\" height=\"550\" title=\"\"><\/a><\/p>\n<h2 id=\"q-how-test-command-handles-symbolic-links\">Q4. How test command handles symbolic links?<\/h2>\n<p>The test command dereferences symbolic links, although there are a couple of exceptions. Following is what the man page says about this:<\/p>\n<pre>Except for -h and -L, all FILE-related tests dereference symboliclinks.<\/pre>\n<p>In case you aren&#8217;t aware, both -h and -L check if a file exists and is a symbolic link &#8211; so their exclusion makes sense, right?<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The test command offers a lot of options, but broadly speaking, you can club them into 3-4 categories. We have provided examples on each category. So try these out, and when you&#8217;re done, head to the utility&#8217;s\u00a0<a href=\"https:\/\/linux.die.net\/man\/1\/test\" target=\"_blank\" rel=\"noopener noreferrer\">man page<\/a> for more info.<\/p>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Flinux-test-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-4.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-test-command%2F&amp;text=Linux+test+Command+Tutorial+for+Beginners+%28with+Examples%29&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-5.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-6.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Flinux-test-command%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/05\/linux-test-command-tutorial-for-beginners-with-examples-7.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, while working on the Linux command line, you might want to test certain things like integer values, or whether or not a file is of certain type? You&#8217;ll be glad to know there&#8217;s a built-in command line utility test that lets you do most of these comparisons and tests. In this tutorial, we will &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-3885","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=3885"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/3885\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=3885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=3885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=3885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}