Bash
25 JulyReturn Array from Function in Bash
Bash cannot return values, whether a single value or an array, but it can return a status (the same as other programs). However, there are multiple turnarounds that we can use to return an array from a function. Let’s explore them below. Using nameref Feature To return the entire array from a function in Bash: […]
25 JulyFormat Date to yyyymmdd in Bash
Using date Command Use the date command to format the date to yyyymmdd in Bash. [crayon-691fc7e68c4c5632252558/] [crayon-691fc7e68c4ce282554212/] In the above example, the bash date command is used to get the current date with the format specifier '%Y%m%d' to specify the date format. In Bash, the date command is used to retrieve the date and time […]
24 JulyGet Last Word in Each Line in Bash
Using awk Command Use the awk command to get the last word of each line from a file in Bash. [crayon-691fc7e68cbf9218347963/] [crayon-691fc7e68cbfe577138714/] [crayon-691fc7e68cbff775110136/] We use the awk command, a powerful text-processing tool allowing you to process and manipulate structured data. The awk command operated each line individually and performed action based on the predefined patterns […]
24 JulyExit 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-691fc7e68d3e2502395889/] [crayon-691fc7e68d3e8898574673/] We […]
24 JulyGet 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-691fc7e68dc12535585718/] [crayon-691fc7e68dc17601625486/] [crayon-691fc7e68dc19473984618/] 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 […]
18 JulyGet Everything After Character in Bash
Using Parameter Expansion Use parameter expansion to get everything after character in Bash. [crayon-691fc7e68dff8896232329/] [crayon-691fc7e68dffc440870733/] 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 […]
18 JulyGet 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 […]
18 Julysed 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-691fc7e68e514248781563/] [crayon-691fc7e68e517388431103/] 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 […]
17 JulyPrint Every nth Line from File in Bash
Using awk Command Use the awk command to print every nth line from file in Bash. [crayon-691fc7e68e711136638973/] [crayon-691fc7e68e714937150613/] [crayon-691fc7e68e716136332363/] [crayon-691fc7e68e717809502521/] 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 […]