• Check if String starts with another String in Bash
    15 March

    Check if String Starts with Another String in Bash

    Using Regex Operator with if statement To check if string starts with another string in bash, use the regex operator =~ with if statement. [crayon-676853c85f398907149650/] [crayon-676853c85f39e139571329/] In this script, the =~ operator compares the string variable to the regular expression. In addition, this operator tests whether the string on the left side matches the regular […]

  • Get Random number between 1 and 100 in Bash
    13 March

    Get Random Number Between 1 and 100 in Bash

    Using $RANDOM Variable Use the $RANDOM variable to generate a random number between 1 and 100 in Bash. [crayon-676853c85f675067968958/] [crayon-676853c85f679621678063/] The above code generates a random integer number between 1 to 100 and prints it on the bash console. How? The $RANDOM generated a random integer between 0 and 32767. Then, we used the %100 […]

  • Check number of arguments in Bash
    11 March

    Check Number of Arguments in Bash

    1. Overview In Bash scripting, it’s often necessary to verify the number of arguments passed to a script to ensure correct script execution. This is crucial in scenarios where the script’s behavior depends on user-provided inputs. In this article, we will see different ways to check number of arguments in bash. 2. Introduction to Problem […]

  • Check if Command exists in Bash
    07 March

    Check if Command Exists in Bash

    Use command Command Use command to check if the specified bash command exists. [crayon-676853c85f9e8805665839/] [crayon-676853c85f9ea387634719/] The command command is used to run a command bypassing any aliases or functions that may be defined. We can also use it to check if a command exists. In the above code, the if statement checks if the curl […]

  • Get Filename without extension in Bash
    07 March

    Get Filename without Extension in Bash

    TL;DR If you know extension of the file, you can use basename command and pass filename and extension as parameters to retrieve filename without extension in bash. This is useful when you have file extension like .tar.gz [crayon-676853c85fabe631951950/] [crayon-676853c85fac0159904231/] Using basename Command Use the basename command to get the filename without extension in Bash. [crayon-676853c85fac1554182077/] […]

  • Check if Array contains value in Bash
    07 March

    Check if Array Contains Value in Bash

    Using echo with grep Command To check if a Bash array contains a value, use the echo command and pipe it to grep command to search the value from the defined array. [crayon-676853c85fc07078925499/] [crayon-676853c85fc0b343096946/] The value in the bash can also be checked with the grep command. We first declared an array named my_array that […]

  • Check if Variable is empty in Bash
    28 February

    Check If Variable Is Empty in Bash

    Checking If Variable Is Empty in Bash There are multiple ways to check if a variable is empty or not. Before moving towards those methods, knowing when a variable is called empty is necessary. In Bash, a variable is called empty if: A variable is declared without assigning any value to it. A variable is […]

  • Echo $1 Bash
    19 February

    What is echo $1 in Bash

    Meaning of the echo $1 in Bash The echo $1 is a bash command; now, those new to Bash can think of it as a famous shell and scripting language in Unix-based operating systems. Using Bash, we can take advantage of multiple features and commands that assist users in automation and system administration tasks. Are […]

  • Remove newline from String in Bash
    19 February

    Remove Newline from String in Bash

    Using tr Command Use the tr command to remove the new line from a string. The tr command is used for character translation or deletion. [crayon-676853c85ffa9130431902/] [crayon-676853c85ffac615629559/] In the above, we have a string that contains a newline character between Hello and World. The echo command displays the string, including the newline character. The | […]