• Bash split string and get last element
    16 April

    Bash Split String and Get Last Element

    Using read Command Use the read command with parameter expansion to split the string and get the last element in Bash. [crayon-663d66d567ba4365534037/] [crayon-663d66d567bab651002501/] In the above example, the read command is used with the ${myString##*:} parameter expansion to get the last element of the string which is then assigned to the lastElement variable using the […]

  • Bash add comma to end of each line
    16 April

    Add Comma to End of Each Line in Bash

    1. Overview Adding characters like commas to the end of each line in a text file is a common operation in file processing and data manipulation. This task can be especially useful in formatting data files for CSV conversion or similar purposes. 2. Introduction to Problem Statement Imagine we have a text file named input.txt […]

  • Bash round to 2 decimal places
    16 April

    Round to 2 Decimal Places in Bash

    1. Overview In Bash scripting, dealing with numbers and specifically rounding them to a certain number of decimal places is a common task. For example, given a number like 3.14159, the goal is to round it to two decimal places, resulting in 3.14. This article explores various methods to achieve this in Bash, focusing on […]

  • Bash log output to file
    16 April

    Bash Log Output to File

    Using Redirect Operator To log the output of a bash command to a file, use the redirect operator. There are two types of redirect Operator: > (Standard Output Redirect Operator): It redirects the output of a command to a file and overwrites the file’s contents. >> (Standard Output Append Operator): It appends the command’s output […]

  • Bash remove special characters from String
    16 April

    Bash Remove Special Characters from String

    Using Parameter Expansion Use the parameter expansion with regex to remove the special characters from a string in Bash. [crayon-663d66d5698d1796078561/] [crayon-663d66d5698d7602517985/] In this example, the parameter expansion removes all special characters from the "Hello World! This is a test string." string. We can observe that our input string contained two special characters, the exclamation marks […]

  • Remove character from String in bash
    16 April

    Remove Character from String in Bash

    1. Overview In this article, we will explore different ways to remove characters in String in different scenarios such as removing specific characters, removing first character, removing last character, and removing first and last characters. 2. Using Parameter Expansion Bash’s built-in parameter expansion is best suited for performing string manipulation operations. Let’s explore parameter expansion […]

  • Bash remove double quotes from String
    15 April

    Remove Double Quotes from String in Bash

    1. Overview Removing double quotes from strings is a s crucial task in many scenarios, such as parsing JSON data, cleaning up user input, or preparing strings for further processing. In this article, we will see different ways to remove double quotes from String using tr, parameter expansion, awk, sed and grep. 2. Introduction to […]

  • Bash replace character in String
    15 April

    Replace Character in String in Bash

    1. Overview In this article, we will explore different ways to replace characters in String in different scenarios such as replacing the first or all occurrences of a given character, replacing all occurrences of multiple characters, and substituting a complete word. 2. Using Parameter Expansion Bash’s built-in parameter expansion can be used to manipulate String […]