Command Substitution in Bourne Shell


Introduction

In world of Unix and Linux, Bourne shell is a popular and widely used command-line interface for interacting with operating system. One of most powerful features of Bourne shell is command substitution, which allows you to use output of one command as input for another. This article will explore command substitution in Bourne shell, including how it works, why it's useful, and some practical examples.

What is Command Substitution?

In Bourne shell, command substitution is a feature that allows you to take output of one command and use it as input for another command. This is done by enclosing command you want to substitute inside a set of backticks (`) or by using dollar sign ($) and parentheses (().

The basic syntax for command substitution is as follows −

command

or

$(command)

Where "command" is command that you want to run and substitute output.

Why Use Command Substitution?

Command substitution is an incredibly useful feature in Bourne shell for a variety of reasons. Firstly, it allows you to simplify complex commands by breaking them down into smaller, more manageable pieces. Secondly, it enables you to automate repetitive tasks by scripting commands that utilize command substitution. Finally, it allows you to access output of one command in another command without having to save output to a file first.

Examples of Command Substitution

To better understand command substitution in Bourne shell, let's look at some practical examples.

Counting Number of Files in a Directory

One common use case for command substitution is counting number of files in a directory. To do this, you can use ls command to list files in directory, and then use wc command to count number of lines in output. command looks like this −

count=$(ls | wc -l)
echo "There are $count files in this directory."

This command uses ls command to list files in current directory, and wc command to count number of lines in output. result is saved in "count" variable, which is then used in echo command to display number of files in directory.

Adding Output of Two Commands

Another common use case for command substitution is adding output of two commands. To do this, you can use command substitution to run two commands and then add their output together. command looks like this −

total=$(($(ls | wc -l) + $(ps | wc -l)))
echo "There are a total of $total processes and files on this system."

This command uses command substitution to run two separate commands: ls and ps. output of each command is then counted using wc command and added together using plus sign. result is saved in "total" variable, which is then used in echo command to display total number of processes and files on system.

Repeating a Command with Command Substitution

Another useful feature of command substitution is ability to repeat a command with different input values. To do this, you can use a for loop and command substitution to run a command multiple times with different input values. command looks like this −

for i in $(seq 1 10)
do
   echo "The square of $i is $(($i * $i))"
done

This command uses seq command to generate a sequence of numbers from 1 to 10. for loop then runs echo command 10 times, each time with a different value of $i. output of command is square of each number in sequence.

Here are some additional subheadings and explanations for a more in-depth understanding of command substitution in Bourne shell.

Nested Command Substitution

One of powerful features of command substitution is that you can nest commands inside each other, allowing you to create complex expressions that can be used as input for other commands. For example −

echo "The last modified date of this file is: $(date -r $(which echo))"

This command uses command substitution to first find location of "echo" command using "which" command, then pass that location as input to "date" command to get last modified date of "echo" command. output of "date" command is then used as input to outer "echo" command to display result.

Quoting with Command Substitution

When using command substitution, it's important to be aware of how quoting can affect output of your commands. For example −

echo "There are $(ls | wc -l) files in this directory."

This command will work fine in most cases, but if there are any spaces or special characters in filenames of files in directory, output of "ls" command will be split into multiple arguments, causing "wc" command to fail. To avoid this, you can use quotes around command substitution −

echo "There are $(ls | wc -l)" "files in this directory."

This will prevent output of "ls" command from being split into multiple arguments and ensure that "wc" command works correctly.

Command Substitution with Variable Assignment

Command substitution can also be used to assign output of a command to a variable. For example −

files=$(ls)
echo "The files in this directory are: $files"

This command uses command substitution to run "ls" command and assign output to "files" variable. output is then displayed using "echo" command.

Conditional Command Substitution

Finally, you can use command substitution in combination with conditional statements to create more complex commands. For example −

result=$(if [ -e "/path/to/file" ]; then echo "File exists."; else echo "File does not exist."; fi)
echo $result

This command uses "if" statement to check if a file exists and then assigns output of either "File exists." or "File does not exist." to "result" variable using command substitution. output is then displayed using "echo" command.

Conclusion

In conclusion, command substitution is an essential feature of Bourne shell that allows you to use output of one command as input for another. It simplifies complex commands, automates repetitive tasks, and saves time by allowing access to output of one command in another command without saving it to a file first. backticks (`) or dollar sign ($) and parentheses (()) are used for command substitution in Bourne shell.

In this article, we explored several practical examples of command substitution, including counting number of files in a directory, adding output of two commands, and repeating a command with different input values. Command substitution is a powerful feature that can significantly enhance your productivity and efficiency when working with Bourne shell.

Updated on: 23-Mar-2023

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements