Bash
- 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-673f359162f97133928291/] [crayon-673f359162f9d248021567/] Alternatively, we can print the array as follows: [crayon-673f359162f9e817984804/] [crayon-673f359162f9f045431787/] In both examples, […]
- 30 June
Bash Return String from Function
Using Substitution Syntax Use substitution syntax represented by $(...) to return string from function in Bash. [crayon-673f3591633a2052845117/] [crayon-673f3591633a6330397077/] You can use local variable as well if you want as below: [crayon-673f3591633a7120794017/] [crayon-673f3591633a8806827440/] 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 […]
- 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 […]
- 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 […]
- 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-673f3591638aa521338580/] [crayon-673f3591638ad395005261/] The -v […]
- 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-673f359163b9c453941784/] [crayon-673f359163ba0263821956/] [crayon-673f359163ba1983458978/] [crayon-673f359163ba2295154925/] In the above example, the grep command searched […]
- 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-673f359163d61126417490/] [crayon-673f359163d63246653771/] the${str} is used to expand the value of the str variable and then appends the string o to it, resulting in the string […]
- 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-673f359163e87026494022/] The mkdir is used to create the directory. The -p created all the intermediate directories if they didn’t exist. The /path/to/directory […]
- 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 […]