Bash Arithmetic Operations You can perform arithmetic operations on numeric values in bash scripts. You can find many options to perform arithmetic operations on bash shell but below example will provide you simplest way. Syntax: ((expression)) ((var1+var2)) ((var1-var2)) ((var1*var2)) ((var1/var2)) Bash Arithmetic Operations Example: For example, take an input of two numeric values and perform all 5 basic arithmetic operations like below:
| #!/bin/bash read -p "Enter numeric value: " n1 read -p "Enter numeric value: " n2 echo "Addition of $n1 + $n2 is = " $((n1+n2)) echo "Subtraction of $n1 - $n2 is = " $((n1-n2)) echo "Division of $n1 / $n2 is = " $((n1/n2)) echo "Multiplication of $n1 * $n2 is = " $((n1*n2)) echo "Modulus of $n1 % $n2 is = " $((n1%n2)) |
Increment and Decrement Operator: Bash also used incremnt (++) and decrement (–) operators. Both uses in two types pre-increment/post-increment and pre-decrement/post-decrement.
| ## Post-increment example $ var=10 $ echo $((var++)) ## First print 10 then increase value by 1 ## Pre-increment example $ var=10 $ echo $((++var)) ## First increase value by 1 then print 11 |
Similarly, try pre-decrement and post-decrement…
Read More User Input In Bash Linux read command is used for interactive user input in bash scripts. This is helpful for taking input from users at runtime. Syntax: read [options] variable_name Example 1: There is no need to specify any datatype for the variables with the read operation. This simply takes an input of any data type values. Try with a sample command. Open bash shell terminal and type following command. read myvar The above command will wait for user input. so just type anything and press enter. Let’s create a…
Read More Command Line Arguments in Shell Script Command line arguments are also known as positional parameters. These arguments are specific with the shell script on terminal during the run time. Each variable passed to a shell script at command line are stored in corresponding shell variables including the shell script name. Synatx: ./myscript.sh ARG1 ARG2 ARG3 ARG4 ARG5 ARG6 ARG7 ARG8 ARG9 ARG10 See the below image to understand the command line values and variables. Here ARG1, ARG2 to ARG10 are command line values, which is assigned to corresponding shell variables.…
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