• Bash get exit code of last command
    24 July

    Exit Code of Last Command in Bash

    Every command we run successfully or unsuccessfully in Bash leaves a particular code behind, which we call an exit code or exit status. We use this code to analyze the last command in Bash. Using $? Variable Use shell variable, $?, to get the exit code of the last-run command in Bash. [crayon-662822b71e0f7621789403/] [crayon-662822b71e113277077439/] We […]

  • Bash get output from python script
    24 July

    Get Output from Python Script in Bash

    Using Substitution Syntax Use Substitution Syntax ($(...)) to get output from a Python script and store it in a Bash variable. [crayon-662822b7294cc111647092/] [crayon-662822b7294d9791264595/] [crayon-662822b7294db772385097/] We used the command substitution syntax,$(command), to capture the output of the Python script (my_script.py) in a Bash variable (result). Here, the command was python my_script.py, where a Python interpreter executed […]

  • Bash get everything after character
    18 July

    Get Everything After Character in Bash

    Using Parameter Expansion Use parameter expansion to get everything after character in Bash. [crayon-662822b729aeb775844383/] [crayon-662822b729af4426494210/] First, we set the string variable with a string type value; then, we initialised the delimiter variable with a space character (" "). The delimiter variable contained the character, after which we wanted to extract the remaining portion of the […]

  • Bash get text between two Strings
    18 July

    Get Text Between Two Strings in Bash

    1. Overview In scripting and programming, extracting specific portions of text from larger strings or files is a common task. In this article, we will see different ways to get text between two String using grep, awk, sed, and bash parameter expansion. 2. Introduction to Problem Statement We are given a String, and we need […]

  • sed remove leading and trailing whitespace
    18 July

    sed Remove Leading and Trailing Whitespace

    Using sed Command with Substitutions Use the sed command with multiple substitutions to remove leading and trailing whitespaces from a single-line string in Bash. [crayon-662822b7300cf606733518/] [crayon-662822b7300d8809751975/] First, we created a variable named str and set its value to a string value which contained leading and trailing whitespaces. Then, we piped the echo $str command to […]

  • Bash get every nth line from File
    17 July

    Print Every nth Line from File in Bash

    Using awk Command Use the awk command to print every nth line from file in Bash. [crayon-662822b7308fd808079983/] [crayon-662822b730904304240225/] [crayon-662822b730906877268984/] [crayon-662822b730907888048132/] In this example, the awk command gets every 4th line from the file test.txt. Here, the file variable contains the file name from where you want to retrieve the lines. Then, the user is prompted […]

  • sed add line to end of file
    17 July

    sed Add Line to End of File

    Using sed with a command: Use sed with the a command to add a line to the file’s end in Bash. [crayon-662822b730bc7860238595/] [crayon-662822b730bcb388963328/] In this example, the sed command is used with the a option to add a line This is the last Line to the end of the file myFile.txt. In Bash, sed is […]

  • Bash sort CSV by column
    14 July

    Sort CSV by Column in Bash

    Bash Sort CSV by Column Using sort Command Use bash’s sort command to sort the CSV file by column. [crayon-662822b732f92153022961/] [crayon-662822b732f9c425518283/] [crayon-662822b732f9d475176316/] In this example, the sort command is used to sort the myFile.csv file by column. Here, the -t parameter is used to specify the field separator, which is , in the above case. […]

  • Bash write variable to file
    14 July

    Bash Write Variable to File

    Using echo Command Use the echo command with redirection operator (>) to write variable to file in Bash. [crayon-662822b7335b6857333810/] [crayon-662822b7335bc891827271/] Use the echo command with the append operator (>>) to append a variable to the end of the specified file in Bash. [crayon-662822b7335bd008782769/] [crayon-662822b7335be035088071/] Use the echo command with the append operator (>>) to append […]