Shell Script to Concatenate Two Strings Brief: This example will help you to concatenate two or more strings variable in a bash script. This tutorial helps you with multiple shell script examples of concatenating strings in a shell script. The first example is a general way to concatenate variables of string. You can simply write all the variable one after another:
| #!/bin/bash # Shell program to concatenate two strings in variable str1="Welcome " str2="TecAdmin!" str3=$str1$str2 echo $str3 |
Output: Welcome TecAdmin! Another Example You can also use += operator to concatenate two strings and store results in the first string.
| #!/bin/bash str1="Welcome " str2="TecAdmin!" str1+=$str2 echo $str1 |
Output: Welcome TecAdmin! One More…
Read More Bash – Check If Two Strings are Equal Brief: This example will help you to understand to check if two strings are equal in a bash script. This shell script accepts two string in variables and checks if they are identical. Details Use == operator with bash if statement to check if two strings are equal. You can also use != to check if two string are not equal. You must use single space before and after the == and != operators. Example In this script two variables are initialized…
Read More Shell Script to Add Two Integers Brief: This example will help you to understand to add two numbers in the bash script. This script takes the input of two numbers from the user and prints the sum of both numbers. This is an example script initializes two variables with numeric values. Then perform an addition operation on both values and store results in the third variable.
| #!/bin/bash # Calculate the sum of two integers with pre initialize values # in a shell script a=10 b=20 sum=$(( $a + $b )) echo $sum |
Command Line Arguments In this second example, shell script reads two numbers as command line parameters and perform the addition operation.
| #!/bin/bash # Calculate the sum via command line arguments # $1 and $2 refers to the first and second argument passed as command line arguments sum=$(( $1 + $2 )) echo "Sum is: $sum" |
Output:…
Read More What is Mongo Shell? MongoDB provides an interactive mongo shell based on JavaScript. This provides a command line interface between users and MongoDB databases. You can find the best tutorials here for the installation of MongoDB server on Linux operating systems. What is mongo Shell? Mongo shell is a command line interface between user and database. You can connect to your MongoDB server using this shell and manage your databases and collections. You can also perform the administrative tasks on MongoDB. Connect to MongoDB Shell Type mongo on your system…
Read More For Loop in Bash In a programming language, a loop is used to repeat the execution of a block of code until the satisfied defined condition. Which is helpful to perform repetitive tasks. Mainly there are 3 types of loops, for, do, and do-while. In this tutorial, we will discuss for loop in shell scripting. The shell scripting also provides for loops to perform repetitive tasks. A basic for loop syntax looks like: Syntax: for VARIABLE in PARAM1 PARAM2 PARAM3 do //for-loop statements done The for loop executes for all…
Read More Quotes in Bash This is a standard practice to quote the string in any programming language. Quotes are used to deal with the texts, filenames with a space character. Read this tutorial to understand the differences between single quote and double quotes. Quote with String While working with simple texts and string, there are no different in using a single quote or double quote.
| #!/bin/bash echo 'String in single quote' echo "String in double quote" mkdir 'Dir 1' mkdir "Dir 2" |
The above script will run without any error and print the messages and create both directories. Quote with Variables Just remember that the shell variable…
Read More Variables in Bash The bash variables are the same as other programming languages. You don’t need to specify the type of variable in bash scripting. Read below example. #!/bin/bash NAME=”TecAdmin Tutorials” echo $NAME launchdate=”Feb 08, 2013″ echo $launchdate Global vs Local Variables Global variables are accessible anywhere in the script. Where Local variables are accessible in scope only. For example, a variable that is used inside a function only.
| #!/bin/bash #Define bash global variable GLOBAL_VAR="global variable value" function hello { #Define bash local variable local LOCAL_VAR="local variable value" echo $LOCAL_VAR echo $GLOBAL_VAR ## This will accessible here } echo $LOCAL_VAR ## This will not accessible here echo $GLOBAL_VAR |
System Variables System variables are responsible to define the aspects of the shell. These variables are maintained by bash itself.…
Read More Shell Scripting The Bash (Born Again) is a Unix shell and command line language written by the Brian Fox. The Born Again shell is used by most of the Unix/Linux operating systems as there default shell. This Shell Script tutorial is written for the peoples want to learn the basics of shell script programming (Shell scripting). In this series of tutorials, you will learn about bash scripting, which is very useful for automating the daily tasks, larger tasks easier. It helps to automation of tasks like backups, disk cleanup etc.
Read More