Linux Reader 4.0Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows
Linux Reader 4.0
Ext 2/3/4, UFS2, HFS and ReiserFS/4 for Windows

What is a Bash Test Command

Here you will find out:

  • what a bash test command is
  • what file tests you can use
  • when DiskInternals can help you

Are you ready? Let's read!

What is a bash test command?

A test command in a shell script is necessary for comparing one element with another (strings, arithmetic operations, etc. can also be compared). Usually, it is initially built into the bash shell and is used by testers in a consistent format.

Description of a test command and its syntax

A shell script test command gives the result of the comparison in a peculiar format: the output state returns 1 for “false” (test failed) or 0 for “true” (test passed successfully).

Also, this command is often used as part of a conditional expression. You should be aware that a test command in shell script is sometimes expressed in single brackets: [...]. However, they should be separated from all other arguments by a space.

Now let's look at the syntax:

[is a synonym for a test, but requires the last argument].

Please note the double brackets [[...]]; denote a new test. Recently, they have been used more often, as they have versatility.

Single brackets are called an old test and are more portable.

You can also use parentheses (()) instead; these allow any shell arithmetic.

Use these file tests

The number of file tests is actually very large, so familiarize yourself with the most common of them shown in the table. Keep in mind that the result is 0 or True if the file actually exists and all of its characteristics match.

Operator syntax Description
-a {FILE} True if {FILE} exists.
-e {FILE} True if {FILE} exists. Approximately the same as -a.
-f {FILE} is a regular file.
-d {FILE} is a directory.
-c {FILE} is a character special file.
-b {FILE} is a block special file.
-p {FILE} a named pipe (FIFO).
-S {FILE} is a socket file.
-L {FILE} is a symbolic link.
-h {FILE}  is a symbolic link.
-g {FILE} has sgid bit set.
-u {FILE} suid bit set.
-r {FILE} is readable.
-w {FILE} is writable.
-x {FILE} is executable.
-s {FILE} and has size larger than 0 (not empty).
-t {fd} True, if file descriptor {fd} is open and refers to a terminal.
{FILE1} -nt {FILE2} True, if {FILE1} is newer than {FILE2} (mtime).
{FILE1} -ot {FILE2} True, if {FILE1} is older than {FILE2} (mtime).
{FILE1} -ef {FILE2} True, if {FILE1} and {FILE2} refer to the same device and inode numbers.

String tests to use and the number of arguments


Operator syntax Description
-z {STRING} True if {STRING} is empty.
-n {STRING} True if {STRING} is not empty
{STRING1} = {STRING2} True if both lines are equal.
{STRING1} ! = {STRING2} True if both lines are not equal.
{STRING1} { {STRING2} True if {STRING1} is sorted to lexicographic {STRING2}
{STRING1} } {STRING2} True {string1} sorts after lexically{STRING2}

Any built-in test at first glance may seem simple, but this is not always the case. Often, a large number of arguments can cause certain difficulties.

The easiest way to open Linux files from Windows

Here we will talk about a unique and rare application: DiskInternals Linux Reader. You need it if you use a virtual machine or have a dual-boot setup, and you need to get files from Linux to Windows. This application has the most secure read-only access to any file in the source disk. The program provides access from Windows to Ext2 / Ext3 / Ext4, HFS, ReiserFS and other Linux file systems. You will also be able to preview files completely free of charge. Also, inexperienced users can use the Wizard to help with migrating files, which will clearly and concisely put everything in order. Therefore, do not even hesitate; you will not find a better option today!

FREE DOWNLOADVer 4.0, Win Upgrade to PROFrom $29.95

0.20973610877991