• Contact
  • About Me
  • Privacy Policy
  • Disclaimer
DefenceDev
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos
No Result
View All Result
DefenceDev
No Result
View All Result
ADVERTISEMENT
Home Linux Tutorials Bash Scripting Tutorials

Lesson 6: Using Bash Command Substitution

neo by neo
May 27, 2025
in Bash Scripting Tutorials
0
Lesson 6: Using Bash Command Substitution

Lesson 6: Using Bash Command Substitution

0
SHARES
9
VIEWS
Share on FacebookShare on Twitter
ADVERTISEMENT

Lesson 6: Using Bash Command Substitution

Welcome to Lesson 6: Using Bash Command Substitution. Bash command substitution is a feature that allows you to execute a command and replace it with its output. This can be incredibly useful for scripting and automating tasks in a Unix-like environment. In this article, we’ll cover how to use command substitution, including its syntax and practical examples to help you get started.

What is Command Substitution?

Command substitution in Bash unlocks the ability to capture a command’s output and incorporate it seamlessly into your scripts or other commands. With the straightforward $(command) or backtick `command` syntax, you can dynamically embed results into variables, filenames, or expressions. For instance, you can insert the current date into log files or fetch system details to enhance your automation tasks. In this lesson, you’ll dive into practical examples and learn how to wield command substitution to streamline your Bash scripting workflows. So, let’s jump in and maximize your scripting efficiency!

You can accomplish this using two different syntaxes:

  • Backticks ( )
result=`command`
  • Dollar sign and parentheses ($()):
result=$(command)

The second syntax is preferred for its readability and ability to be nested.

Basic Examples

Using date command:

current_date=$(date)
echo "Today's date is: $current_date"

Using ls command:

files=$(ls)
echo "Files in the current directory: $files"

Nested Command Substitution

One of the advantages of using the $() syntax is that it allows for nesting. Here’s an example:

ADVERTISEMENT
nested_result=$(echo $(date))
echo "Nested command substitution result: $nested_result"

Step-by-Step Breakdown:

  1. Run the Inner Command ($(date)):
    • The date command executes first because it’s nested inside the echo command.
    • It produces the current date and time as a string (e.g., Fri Jan 17 12:45:30 UTC 2025).
  2. Pass Result to Outer Command (echo $(date)):
    • The output from $(date) goes to the echo command as an argument.
    • The echo command prints the date and time to standard output.
  3. Assign Result to nested_result:
    • Bash stores the final output of the echo command (Fri Jan 17 12:45:30 UTC 2025) in the variable nested_result.

Output:

ADVERTISEMENT
Nested command substitution result: Thu Jul 25 09:14:58 AM UTC 2024

Practical Applications

Command substitution can enhance your scripts’ efficiency in various scenarios, such as:

ADVERTISEMENT
  • Capturing command output for conditional statements:
if [ $(whoami) = 'root' ]; then
    echo "You are the root user."
else
    echo "You are not the root user."
fi
  • Using command output in loops:
for file in $(ls); do
    echo "Processing file: $file"
done

defencedev.com


«Previous: Lesson 5: Linux Bash Read User Input with Example
Next: Lesson 7: Bash Functions with Examples

About The Author

neo

See author's posts

Tags: bash-scripting
ADVERTISEMENT
Previous Post

Lesson 5: Linux Bash Read User Input with Example

Next Post

Lesson 7: Bash Functions with Examples

neo

neo

Related Posts

Lesson 11: Practical Linux Bash Examples and Projects
Bash Scripting Tutorials

Lesson 11: Practical Linux Bash Examples and Projects

Lesson 10: Bash File Operation (Read, Write, Copy)
Bash Scripting Tutorials

Lesson 10: Bash File Operation (Read, Write, Copy)

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples
Bash Scripting Tutorials

Lesson 9: Bash Maths Operations (Bash Arithmetic) Examples

Lesson 8: Working with Bash Strings
Bash Scripting Tutorials

Lesson 8: Working with Bash Strings

Lesson 7: Bash Functions with Examples
Bash Scripting Tutorials

Lesson 7: Bash Functions with Examples

Lesson 5: Linux Bash Read User Input with Example
Bash Scripting Tutorials

Lesson 5: Linux Bash Read User Input with Example

Next Post
Lesson 7: Bash Functions with Examples

Lesson 7: Bash Functions with Examples

Recommended

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Install Docker on Raspberry Pi 4 with Ubuntu 22.04

Remote Desktop Access to Ubuntu 24.04 from Windows

Remote Desktop Access to Ubuntu 24.04 from Windows

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script

FortiGate DDoS Protection: Configure DoS Policies to Secure Self-Hosted Applications

FortiGate DDoS Protection: Configure DoS Policies to Secure Self-Hosted Applications

Fortinet FortiGate Site-to-Site IPsec VPN Troubleshooting

Fortinet FortiGate Site-to-Site IPsec VPN Troubleshooting

ADVERTISEMENT

DefenceDev Tutorials

defencedev Logo

Whether you’re just starting or looking to expand your skills, I hope you find useful information and engaging discussions here. Let me take you through my journey and the goals behind this space!

Follow Us

Recent News

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script

Protect Docker-Hosted Application from HTTP Flood (DDoS) Attacks Using a Bash Script

FortiGate DDoS Protection: Configure DoS Policies to Secure Self-Hosted Applications

FortiGate DDoS Protection: Configure DoS Policies to Secure Self-Hosted Applications

  • Site Map
  • Privacy Policy
  • Facebook Page
  • Disclaimer
  • Contact
  • About Me

© 2025 defencedev.com - All rights reserved.

No Result
View All Result
  • Home
  • Blog
  • Linux Tutorials
    • Bash Scripting Lessons
    • Commands
    • Networking
  • Solutions
    • Docker
  • Network Tutorials
    • FortiGate
    • MikroTik
  • Projects
    • AdGuard
    • Immich
    • Nextcloud
    • WordPress
  • Cloud
  • Video Tutorials
    • YouTube Channel
    • MikroTik Videos

© 2025 defencedev.com - All rights reserved.