Bash
29 AugustBash Replace Space with Newline
If you have multiple consecutive spaces in the string, use the last solution, the for loop. You can use any of the following solutions if you have a multiline string. Using tr Command Use the tr command to replace space with newline in bash. text="This is a sample string." new_text=$(echo "$text" | tr ' ' […]
26 NovemberCheck If File Contains String in Bash
1. Overview Searching for strings in text files is a common task in bash, used in scenarios like log file analysis and configuration file searches. This article explores various methods to check if file contains String, including both case-sensitive and case-insensitive approaches. 2. Introduction to Problem Statement Let’s consider a log file named server.log: 2023-11-24 […]
20 AugustCheck If Output Contains String in Bash
1. Overview In this article, we will see how to check if output contains String in Bash using grep, Conditional Expressions, awk, sed commands with various options. 2. Introduction to Problem Statement We will use ls -l to list directories and files in long format and search for .txt string in the output. For example: […]
02 AugustCheck 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. This is Line number 1. This is Line Number 2. It is Line number 3. I am Line 4. This is Line number 1. This is Line Number 2. It is Line number 3. I […]
02 AugustGet 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 AugustCheck If Boolean Is True in Bash
Using if Statement We can use an if statement with different variations to determine whether the provided Boolean variable is true. Note that the examples covered in this section will be similar. We will learn the first example in Use if with true Condition in detail, but later, we will discuss the section which will […]
02 AugustCheck If Host Is Reachable in Bash
Using ping Command Use the ping command in bash to check if the host is reachable or not. #!/bin/bash host="example.com" ping -c 1 $host > /dev/null if [ $? -eq 0 ]; then echo "Host is reachable." else echo "Host is not reachable." fi Host is reachable. In this bash script, the ping command is […]
29 JulySet 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. date_variable=$(date) echo "Today's date: $date_variable" Today's date: Thu Jun 8 04:08:47 UTC 2023 We used the date command to get the current date and time. The date command was enclosed within the substitution syntax represented by […]
28 JulyConvert 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. array=("Jan" "Feb" "Mar" "Apr") printf -v joined '%s,' "${array[@]}" echo "${joined%,}" Jan,Feb,Mar,Apr 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 […]
- 1
- 2
- 3
- …
- 5
- Nextkeyboard_arrow_right