• Bash add character to String
    04 May

    Bash Add Character to String

    We can also use these methods to add a string to the given string as well. Using $ Operator Use the $ operator to add character to string in Bash. [crayon-661ec8c6daa85187424845/] [crayon-661ec8c6daa92246706255/] the${str} is used to expand the value of the str variable and then appends the string o to it, resulting in the string […]

  • Create folder if not exists in Bash
    03 May

    Bash Create Folder if Not Exists

    To run the following commands, directly write the command in the bash terminal and execute them. Using mkdir -p Command Use the mkdir -p command to create folder if not exists in Bash [crayon-661ec8c73cc28408836056/] The mkdir is used to create the directory. The -p created all the intermediate directories if they didn’t exist. The /path/to/directory […]

  • If not condition in bash
    02 May

    If Not Condition in Bash

    In Bash, figuring out how you interpret the term if not condition before digging into the solutions is mandatory. For example, are you taking as negating the condition/expression, which means make the condition/expression False if it is True and vice versa? Or, you want to make some comparisons to assess inequality; this comparison can be […]

  • 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-661ec8c74a83c902003466/] [crayon-661ec8c74a843471333011/] 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-661ec8c77ab32457405434/] [crayon-661ec8c77ab38907880941/] 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 […]

  • Bash get absolute path from relative path
    16 April

    Bash Get Absolute Path from Relative Path

    When working with shell scripts, it’s common to need to get the absolute path of a file or directory from its relative path. However, before we dive into how to get the absolute path from a relative path, we must learn the difference between the two types of paths. The relative path is relative to […]