Author: Arpit Mandliya
- 02 August
Check if Two Files are the Same in Bash
In this entire article, we will use the following content of three different text files to practice the provided solutions. [crayon-67266eaa8ebe4860452074/] [crayon-67266eaa8ebef237688464/] [crayon-67266eaa8ebf1572384686/] Using cmp Command Use the cmp command to check if two files are the same in Bash. [crayon-67266eaa8ebf2645905099/] [crayon-67266eaa8ebf3617624594/] In the above example, we initialized the file1 and file2 variables with the […]
- 02 August
Get Last Line of Output in Bash
1. Overview In this article, we will see how to get last line of output in Bash using tail, awk, sed, mapfile, and head commands with various options. 2. Introduction to Problem Statement We will use ls -l to list directories and files in long format and get the last line of output using various […]
- 02 August
Check If Host Is Reachable in Bash
Using ping Command Use the ping command in bash to check if the host is reachable or not. [crayon-67266eaa8fe6f673933291/] [crayon-67266eaa8fe73537775754/] In this bash script, the ping command is used to check if the host "example.com" is reachable or not. In bash, the ping command is used to send ICMP Echo Request packets to the given […]
- 29 July
Set Output of Command to Variable in Bash
Using Substitution Syntax Use substitution syntax to capture the output of a single command into a variable in Bash. [crayon-67266eaa9001f525252498/] [crayon-67266eaa90023107610392/] We used the date command to get the current date and time. The date command was enclosed within the substitution syntax represented by the $(...), which we used to capture the output of the […]
- 28 July
Convert Array to Comma Separated String in Bash
Using printf Statement Use the printf statement to convert the array to a comma-separated string in Bash. [crayon-67266eaa90237304167549/] [crayon-67266eaa9023a923637477/] We initialized an array containing string elements. Then, we used the printf statement to convert the specified array to a comma-separated string. How? The printf statement formatted and stored the elements of the array into the […]
- 25 July
Return Array from Function in Bash
Bash cannot return values, whether a single value or an array, but it can return a status (the same as other programs). However, there are multiple turnarounds that we can use to return an array from a function. Let’s explore them below. Using nameref Feature To return the entire array from a function in Bash: […]
- 25 July
Merge Arrays in PowerShell
Using + Operator Use + operator to merge arrays in PowerShell. [crayon-67266eaa90b6a805277125/] [crayon-67266eaa90b6f681565999/] We used the sub-expression operator represented by @() to create two separate arrays named $array1 and $array2 (you can also create arrays without using the sub-expression operator). Note that both arrays contained string-type values. Next, we used the + operator to join […]
- 25 July
Format Date to yyyymmdd in Bash
Using date Command Use the date command to format the date to yyyymmdd in Bash. [crayon-67266eaa90ec7512672667/] [crayon-67266eaa90eca174228815/] In the above example, the bash date command is used to get the current date with the format specifier '%Y%m%d' to specify the date format. In Bash, the date command is used to retrieve the date and time […]
- 24 July
Get Last Word in Each Line in Bash
Using awk Command Use the awk command to get the last word of each line from a file in Bash. [crayon-67266eaa91add618327184/] [crayon-67266eaa91ae2147584486/] [crayon-67266eaa91ae3497695038/] We use the awk command, a powerful text-processing tool allowing you to process and manipulate structured data. The awk command operated each line individually and performed action based on the predefined patterns […]