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!
Related articles
- About a bash date command
- How to Access Linux Ext2 or Ext3 on Windows
- How to Access Ext4 from Windows
- How to Mount Ext4 on Windows for Free
- Mount Linux Drive on Windows for Free
- Bash Script: All You Need to Know
- An Algorithm: How to Create Bash While Loop
- Linux Shell: What You Need to Know at First
- 5 Basic Shell Script Examples for Your First Script
- Bash: How to Check if the File Does Not Exist
- Bash Time Command on Linux
- How to use bash get script directory in Linux
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Bash Script SSH: How to Use It
- Bash: A Script For User Input
- Linux ZSH: the basic you need to know
- Basic of Grep in Linux Shell Script
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Bash: how to split strings in Linux
- Linux: Sudo in Bash Scripts
- Hello World Script in Bash
- A Linux bin/bash Shell
- Linux: New Line in Shell Script
- Linux: Bash String Ends With
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- Linux: Write a Shell Script
- Shell Script: Replace String in File
- A crontab service shell script
- Bash and sh: is there any difference
- A Bash Dirname Command
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- Linux: A Bash Linter
- A Bash Nested Loop
- Use a Shell Script to Send an Email
- A Bash Status of Last Command
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- About Bash Language
- Bash for Loop in One Line
- AWK in a Bash Script
- Learn about useful bash scripts
- Learn about systemd startup script
- About chaining bash commands
- Learn about a bash error code
- Using a bash tee command
- About a bash export command
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash UNTIL loop
- Learn about C shell script
- Learn about Korn shell scripting
- Using /usr/bin/env command
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about SFTP in bash scripting
- Learn to run Perl script in Linux
- Examples of using the Expect
- Copy command in shell scripts
- Shell script usage
- Install Oracle Database
- Here is everything you need to know about Bash for Windows
- Guide in Pictures
- How to Use Shell Script Sleep Command
- A Bash Multiline Command




