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-676810117ea6e315326581/] [crayon-676810117ea76657887294/] Alternatively, we can print the array as follows: [crayon-676810117ea78588443417/] [crayon-676810117ea79274915651/] 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-676810117f0f0078939154/] [crayon-676810117f0f8577056073/] You can use local variable as well if you want as below: [crayon-676810117f0f9408551373/] [crayon-676810117f0fa230551414/] 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-676810117f700687378746/] [crayon-676810117f707402633039/] 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-676810117fbb1607340767/] [crayon-676810117fbbd439774288/] [crayon-676810117fbbf494672622/] [crayon-676810117fbc1903231427/] 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-676810117fe13918157644/] [crayon-676810117fe17670110636/] 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-676810117ffaf558069286/] 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 […]