Benefits:
Some benefits of using bash expansion are mentioned here.-
- Users can automate repetitive tasks.
-
- Improve efficiency, and enhance productivity when working with the Bash shell.
-
- It allows for quicker manipulation of strings, simplifies complex commands, and provides a more efficient way of processing data.
-
- It helps users write cleaner and more concise scripts, reducing the risk of errors and improving overall code readability.
Bash substitution:
Bash substitution refers to the process of replacing certain elements within a string with other values or commands in the Bash shell. This can be done using various techniques such as variable expansion, command substitution, arithmetic expansion, and wildcard matching. It allows users to dynamically modify and manipulate strings, making their tasks more efficient and streamlined.Benefits:
Some benefits of using bash substitution sre mentioned here.-
- Bash shell provides users with the ability to dynamically change and manipulate strings, making their tasks more efficient and streamlined.
-
- It allows for easier automation of tasks, improved readability of scripts, and a more concise way of processing data.
-
- It also helps reduce the risk of errors and speeds up the execution of commands, ultimately enhancing productivity and efficiency in working with the shell.
Types of bash expansion:
Three different types of bash expansions are discussed here with examples.A. Parameter expansion
Parameter expansion is used in Bash shells to manipulate and work with variables in various ways. It offers several options for expanding and modifying variable values, such as extracting substrings, replacing text, and more. It’s a powerful tool for enhancing the functionality and flexibility of shell scripts in Bash. Syntax: 1.${variable} – This syntax simply refers to the value of the variable “variable”. Example: Run the following commands from the terminal. A string value is assigned into a variable and the value of the variable is printed by using parameter expansion.$ variable="Hello" $ echo ${variable}Output: 2. ${variable:-default} – If “variable” is not set or is null, this will use the default value but not assign the default value in the variable. Example: Run the following commands from the terminal. Here, variable2 is not set and a default value, ‘test’ , has been assigned temporarily in the variable.
$ echo $variable2 $ echo ${variable2:-test} $ echo $variable2Output: 3. ${variable:=default} – It is similar to the previous example, but assigns the default value to the variable. Example: Run the following commands from the terminal. Here, variable3 is not set and a default value, ‘test’ , has been assigned permanently in the variable.
$ echo $variable3 $ echo ${variable2:=test} $ echo $variable3Output: 4. ${variable:+value} – If “variable” is set and not null, this will use the value “value”. Example: Run the following commands from the terminal. Here, variable4 is not set initially and a default value, ‘test’ , will be assigned in the variable if the variable4 is set.
$ echo $variable4 $ echo ${variable4:+test} $ variable4="World" $ echo ${variable4:+test}Output: 5. ${variable:offset:length} – This syntax extracts a substring of a specified length starting from a certain position in the variable “variable”. Example: Run the following commands from the terminal. The variable5 is not set initially. Next, a string value has been assigned to the variable and a substring has been extracted from the string value by using parameter expansion. The string value is ‘Bash Script’ and the character at the position 0 is ‘S’. The substring is ‘Script’ after taking 6 characters.
$ echo $variable5 $ variable5="Bash Script" $ echo ${variable5:5:6}Output:
B. Tilde expansion
Tilde expansion is a feature in Unix-like operating systems that expands the tilde (~) character to represent the current user’s home directory. It is commonly used in command line interfaces to quickly navigate to specific directories without having to type out the full path. Some examples of tilde expansion are mentioned below.Example-1: Move to current user’s home directory
Run the following commands to move the temp directory and go to the current user’s home directory by using tilde expansion.$ cd temp $ cd ~Output:
Example-2: Print all files and folders of the current user’s home directory
Run the `ls` command with the tilde expansion to get the output.$ ls ~Output:
Example-3: Read the content of the particular folder
Run the following command to get the content of the temp directory.$ ls ~/tempOutput:
C. Brace expansion
Brace expansion in bash is used to generate arbitrary strings by specifying a template containing curly braces. When the template is expanded, the braces are replaced with all possible combinations of the strings inside them. This can be a useful tool for generating lists of files or directories. Brace expansion can be a handy feature for quickly generating lists or sets of strings in bash scripting. Some examples of brace expansion in bash are mentioned below.Example-1: Print multiple string values
Run the following command to print all characters from A to H$ echo {A..H}Output:
Example-2: Create multiple empty files at a time
Run the following command to create five text files with the names testfile01.txt, testfile02.txt, testfile03.txt, testfile04.txt, and testfile05.txt.$ touch testfile{01..05}.txtOutput:
Types of bash substitution:
Two different types of bash substitutions are discussed here with examples.A. Command substitution
Command substitution in Bash allows one to run a command and use its output as part of another command or stored in a variable. It is typically done using the `$()` syntax or backticks(“). Some examples of command substitution are mentioned below.Example-1: Store command output into a variable
Run the following commands to store the output of the pwd command by using `$()` into a variable and print the variable later with another string.$ cur_dir=$(pwd) $ echo "The current working directory is $cur_dir"Output:
Example-2: Passing the output of a command to another command
Run the following commands to check the list of the existing text files and count the total number of text files in the current location by using `$()`.$ ls *.txt $ echo "Total text files are $(ls *.txt | wc -w)"Output:
Example-3: Command substitution using backticks
Run the following commands to store the current date value into a variable by using backticks(`) and print the value of the variable.$ today=`date '+%d-%m-%Y'` $ echo "Today is $today"Output:
B. Process substitution
Process substitution in Bash allows one to run a command and use its output as a file-like input to another command. It is typically done using the `<()` syntax. It works like pipe (|). It is a useful tool for working with commands that require input from a file or standard input. Some examples of process substitution are mentioned below.Example-1: Comparing the content of two files
Create two text files named f1.txt and f2.txt. Run the `diff` command with the process substitution to compare the content of these files. Suppose the f1.txt and f2.xt contain the following content. f1.txt Bash is a scripting language. It has many commands to do automated tasks. f2.txt Bash is a scripting language. It is used for shell programming. Run the following commands to check the content of the f1.txt and f2.txt files. Next, find out the differences between these files by using process substitution and `diff` command.$ cat f1.txt $ cat f2.txt $ diff <(cat f1.txt) <(cat f2.txt)Output:
Example-2: Search in a file based on pattern
Run the following commands to check the content of the f1.txt file and search if the word, ‘bash’ and ‘Bash’ exists in the file or not by using process substitution and `grep` command.$ cat f1.txt $ grep "bash" <(cat f1.txt) $ grep "Bash" <(cat f1.txt)Output:
Example-3: Count the total lines and words of a command output
Run the following commands to check the list of all xml files and count the total number of lines of the output and the total number of xml files.$ ls -l *.xml $ wc -lw <(ls -l *.xml)Output: Exercise:
-
- Write the bash script to print the string ‘Hello World’ if the variable is not set.
-
- Write the bash script to assign, ‘Bangladesh’ into a variable and print the word, ‘Bangla’ after extracting the string.
-
- Write the bash script to read the content of the particular folder without moving to that folder without using
ls
command.
- Write the bash script to read the content of the particular folder without moving to that folder without using
-
- Write the bash script to print the current time with AM/PM by using command substitution.
-
- Write the bash script to search those lines from a file that contains numeric data at the end of the line.
Bash expansion: