Linux Comm Test
[ Linux Command Manual](#)
* * *
`test` is a built-in command in Linux/Unix systems used for conditional evaluation. It is mainly used for various testing and comparison operations in shell scripts. It can check file attributes, compare strings and numbers, making it an indispensable tool when writing shell scripts.
* * *
## Basic Syntax
test EXPRESSION # Or use bracket format (more commonly used)
> **Note**: When using bracket format, there must be spaces between the expression and brackets, i.e., ``
* * *
## Main Function Categories
### 1. File Testing
Check various attributes of files or directories:
| Expression | Meaning |
| --- | --- |
| `-e file` | File exists |
| `-f file` | Is a regular file (not a directory or device file) |
| `-d file` | Is a directory |
| `-s file` | File size is not empty |
| `-r file` | File is readable |
| `-w file` | File is writable |
| `-x file` | File is executable |
| `-L file` | File is a symbolic link |
**Example**:
## Example
if[-f"/etc/passwd"]; then
echo"This is a regular file"
fi
* * *
### 2. String Comparison
Compare the relationship between two strings:
| Expression | Meaning |
| --- | --- |
| `-z "string"` | String length is 0 |
| `-n "string"` | String length is not 0 |
| `"str1" = "str2"` | Strings are equal |
| `"str1" != "str2"` | Strings are not equal |
**Example**:
## Example
read-p"Enter username: " username
if[-z"$username"]; then
echo"Username cannot be empty"
fi
* * *
### 3. Numeric Comparison
Compare the size relationship between two integers:
| Expression | Meaning |
| --- | --- |
| `n1 -eq n2` | Equal |
| `n1 -ne n2` | Not equal |
| `n1 -gt n2` | Greater than |
| `n1 -ge n2` | Greater than or equal |
| `n1 -lt n2` | Less than |
| `n1 -le n2` | Less than or equal |
**Example**:
## Example
if[ $(id -u)-eq 0]; then
echo"Current user is root"
fi
* * *
### 4. Logical Operators
Combine multiple test conditions:
| Operator | Meaning | Example |
| --- | --- | --- |
| `!` | Logical NOT | `[ ! -f file ]` |
| `-a` | Logical AND | `` |
| `-o` | Logical OR | `` |
**Modern Recommended Usage** (using `&&` and `||`):
## Example
&&# File exists and is readable
||# Is a directory or is a file
* * *
## Advanced Usage
### 1. Compound Condition Evaluation
## Example
if[-f"/etc/passwd"-a-r"/etc/passwd"]; then
echo"File exists and is readable"
fi
### 2. Combined with if Statement
## Example
if["$1" = "start"]; then
echo"Starting service..."
elif["$1" = "stop"]; then
echo"Stopping service..."
else
echo"Invalid parameter"
fi
### 3. Use in Variable Assignment
## Example
[-z"$PATH"]&&PATH="/usr/bin:/bin"# Set default value if PATH is empty
* * *
## Common Errors and Notes
**Space Issues**:
## Example
["$var" = "value"]# Correct
["$var"="value"]# Incorrect, missing spaces
**String Variable Quoting**:
## Example
[-n"$var"]# Correct, prevents errors when variable is empty
[-n$var]# Will cause error when $var is empty
**Numeric vs String Comparison**:
## Example
# Correct, numeric comparison
["10">"2"]# Incorrect, this is string comparison (lexicographic order)
**File Testing Paths**:
## Example
[-f"$file"]# Correct, variable is quoted
[-f$file]# Will cause error when path contains spaces
* * *
## Practical Application Examples
### Example 1: Check if Backup Directory Exists
## Example
backup_dir="/var/backups"
if[!-d"$backup_dir"]; then
mkdir-p"$backup_dir"
echo"Created backup directory: $backup_dir"
fi
### Example 2: Validate User Input
## Example
read-p"Please enter age: " age
if["$age"-lt 0-o"$age"-gt 120]; then
echo"Invalid age input"
exit 1
fi
### Example 3: Service Start Script
## Example
#!/bin/bash
if["$1" = "start"]; then
if[-f"/var/run/service.pid"]; then
echo"Service is already running"
else
echo"Starting service..."
# Start command
fi
elif["$1" = "stop"]; then
# Stop logic
else
echo"Usage: $0 {start|stop}"
fi
* * *
## Summary
The `test` command is a fundamental tool in shell script programming. Mastering it allows you to:
* Implement conditional branching logic
* Verify file and directory status
* Compare strings and numbers
* Build complex evaluation conditions
Remember key points:
1. Bracket format is more commonly used but pay attention to spaces
2. Variable references should be enclosed in double quotes
3. Distinguish between string comparison and numeric comparison operators
4. Use modern `&&` and `||` instead of `-a` and `-o`
[ Linux Command Manual](#)
YouTip