Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Build complete path in Linux by concatenate two strings?
In Linux systems, path concatenation is a common task when building complete file paths from separate directory and filename components. This process involves joining two or more path segments while handling special cases like trailing slashes, empty strings, and relative paths.
Basic String Concatenation for Paths
The simplest approach to combine paths is using variable substitution. Let's examine a practical example:
$ my_home_dir="/home/shubh/baeldung/" $ repo_path="tutorials/linux-bash/command-line-arguments/src/main/bash" $ file_path="$my_home_dir$repo_path" $ echo $file_path /home/shubh/baeldung/tutorials/linux-bash/command-line-arguments/src/main/bash
However, this basic approach can create issues with multiple consecutive slashes:
$ file_path="$my_home_dir/$repo_path" $ echo $file_path /home/shubh/baeldung//tutorials/linux-bash/command-line-arguments/src/main/bash
While Linux handles multiple slashes without errors, it's better practice to clean them up for clarity:
$ ls -lrt $(realpath ${file_path})/users-loop.sh
-rw-r--r-- 1 shubh shubh 86 May 3 18:07 /home/shubh/baeldung/tutorials/linux-bash/command-line-arguments/src/main/bash/users-loop.sh
Handling Multiple Slashes
The realpath command automatically resolves multiple slashes and normalizes paths:
$ repo_base_dir="${my_home_dir}///tutorials"
$ echo $repo_base_dir
/home/shubh/baeldung////tutorials
$ ls -ld $(realpath ${repo_base_dir})/linux-bash
drwxr-xr-x 1 shubh shubh 512 May 3 18:07 /home/shubh/baeldung/tutorials/linux-bash
A Generic Solution for Special Cases
A robust path concatenation function must handle empty strings, relative paths, and multiple slashes:
concatenate_paths() {
base_path=${1}
sub_path=${2}
full_path="${base_path:+$base_path/}$sub_path"
full_path=$(realpath ${full_path})
echo $full_path
}
The key feature is the parameter expansion ${base_path:+$base_path/}, which adds the base path and trailing slash only if base_path is not empty.
Testing the Generic Solution
Let's test various edge cases:
$ concatenate_paths "/home/shubh//" "baeldung//linux/" /home/shubh/baeldung/linux $ concatenate_paths "" "/home/shubh//baeldung/linux/foo.bar" /home/shubh/baeldung/linux/foo.bar $ concatenate_paths "/home/shubh//baeldung/" "./linux/foo.bar" /home/shubh/baeldung/linux/foo.bar $ concatenate_paths "/home/shubh//baeldung/linux" "../linux/foo.bar" /home/shubh/baeldung/linux/foo.bar
Key Features of the Solution
| Feature | Handling Method | Example |
|---|---|---|
| Multiple slashes |
realpath normalization |
path//to///file ? path/to/file
|
| Empty base path | Parameter expansion |
"" + "file" ? file
|
| Relative paths |
realpath resolution |
./dir/../file ? absolute/path/file
|
| Trailing slashes | Automatic handling |
dir/ + file ? dir/file
|
Conclusion
Path concatenation in Linux requires careful handling of edge cases like empty strings, multiple slashes, and relative paths. The generic solution using parameter expansion and realpath provides a robust method for combining paths safely in shell scripts.
