0
0
Bash Scriptingscripting~15 mins

Process substitution (<() and >()) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Process Substitution in Bash Scripts
📖 Scenario: You work with text files and want to compare their contents quickly without creating temporary files.
🎯 Goal: Learn how to use process substitution &lt;() and &gt;() in bash to handle command outputs as files.
📋 What You'll Learn
Create variables holding file names
Use process substitution to compare file contents
Redirect command output using process substitution
Print the comparison result
💡 Why This Matters
🌍 Real World
Process substitution helps you compare or process data streams easily without creating temporary files, saving time and disk space.
💼 Career
Many system administrators and developers use process substitution in bash scripts to automate file comparisons and data processing efficiently.
Progress0 / 4 steps
1
Create two text files
Create two variables called file1 and file2 with values "file1.txt" and "file2.txt" respectively. Then create these files with the following contents: file1.txt contains "apple\nbanana\ncherry" and file2.txt contains "apple\nbanana\norange".
Bash Scripting
Need a hint?

Use echo -e with newline characters \n to write multiple lines to files.

2
Set up a variable for the diff command
Create a variable called diff_cmd and set it to the string "diff".
Bash Scripting
Need a hint?

Just assign the string "diff" to the variable diff_cmd.

3
Use process substitution to compare files
Use the variable diff_cmd with process substitution to compare the contents of file1 and file2. Write a command using $diff_cmd and process substitution <(cat "$file1") and <(cat "$file2").
Bash Scripting
Need a hint?

Use diff_output=$($diff_cmd <(cat "$file1") <(cat "$file2")) to capture the diff output.

4
Print the diff result
Print the variable diff_output using echo.
Bash Scripting
Need a hint?

Use echo "$diff_output" to show the differences.