• Bash Print Array
    30 June

    Bash Print Array

    Bash Print Array [ Entire array] There are multiple ways to print array in Bash; let’s learn them below. Use echo with Parameter Expansion Use the echo command with parameter expansion syntax to print the array on the Bash console. [crayon-664b7633b6b81506936089/] [crayon-664b7633b6b8a489238411/] Alternatively, we can print the array as follows: [crayon-664b7633b6b8b127835372/] [crayon-664b7633b6b8c487472176/] In both examples, […]

  • Bash return String from function
    30 June

    Bash Return String from Function

    Using Substitution Syntax Use substitution syntax represented by $(...) to return string from function in Bash. [crayon-664b7633b778c736088251/] [crayon-664b7633b7795426293510/] You can use local variable as well if you want as below: [crayon-664b7633b7797916037848/] [crayon-664b7633b7798277492021/] In this example, the return_string() function initialized a local variable named local_str_var with the "Java2Blog" value. Then, we used an echo command to […]

  • Bash echo multiple lines
    30 June

    Echo Multiple Lines in Bash

    1. Overview In this article, we will see how to echo multiple lines, primarily using the echo command in various forms. Additionally, we will look at alternative methods like printf and here documents. 2. Introduction to Problem Statement Our goal is to print multiple lines with various options. The output of every method will be […]

  • Bash remove blank lines from file
    29 June

    Bash Remove Blank Lines from File

    We can use any of the below commands if we are required to write the output to a new file. But, we can only use sed and perl commands if instructed to remove blank lines and lines containing whitespace characters and store the updated content in the original file. Using grep Command Use the grep […]

  • Bash check if environment variable is set
    13 June

    Bash Check If Environment Variable Is Set

    Using if-else with -z,-v,-n Options Use the if-else statement with the -v option to check if an environment variable is set in bash. The echo command in the if block will be executed if the [-v HOME] will be true, and it will be true if the specified variable is set. [crayon-664b7633b875d600701285/] [crayon-664b7633b8762000045476/] The -v […]

  • Bash Check if Grep result is empty
    11 May

    Bash Check If grep Result Is Empty

    All the code snippets are written in the MyScript.sh file, while the dummy.txt file contains some sample data we used in our scripts. Using -q Option with grep Use the -q option with grep to check if the grep command result is empty. [crayon-664b7633b8f5e026435390/] [crayon-664b7633b8f66605866964/] [crayon-664b7633b8f67745574293/] [crayon-664b7633b8f68695678186/] In the above example, the grep command searched […]

  • 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-664b7633b96a1864461908/] [crayon-664b7633b96a7924325121/] 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-664b7633b9d4f538613723/] 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 […]