Bash Read File Into Variavle Then Replace Variables
Variables work every bit temporary storage for whatsoever programming language. The coder needs to take a clear concept of using dissimilar variables in the code. Variables are used to store specific data. The most unremarkably used information blazon of variables are integer, string, float, double and Boolean. The data blazon of whatsoever variable has to be defined at the time of variable declaration for strongly type programming languages. But Bash is a weakly typed programming language that does not require to define any information type at the time of variable declaration. And so when any numeric value assigns to a variable then it volition work equally integer and when whatsoever text value assigns to a variable and so it is cord. BASH variables can be used from final or on whatsoever BASH file. The apply of different types of Bash variables are described in this tutorial by using many examples.
Using variable from control line or last
You don't accept to use whatsoever special grapheme earlier the variable proper noun at the fourth dimension of setting value in BASH like other programming languages. Simply you have to employ '$' symbol before the variable name when you want to read data from the variable. You can fix and become data from a variable from the final in the following fashion.
Case-ane: Declaring and reading string data using variable
Run the post-obit commands from the concluding.
$ myvar="BASH Programming"
$ repeat $myvar
Output:
Example-2: Combining two cord variables
You lot don't have to apply any operator to combine two or more than strings like other languages. Hither, $var1 is used to store string value and $var2 is used to shop a numeric value. Run the following commands from the terminal to combine two variables $var1 and $var2.
$ var1="The price of this ticket is $"
$ var2=50
$ echo $var1 $var2
Output:
**Note: You can impress the value of the variable without any quotation simply if you use quotations then yous have to use double quotations.
Example-3: Concatenating strings with variables
Double quotation can be used to read the value of the variable. In this instance, single quotation is used on one repeat statement and double quotation is used on some other echo statement. Run the following commands from the terminal to check the output.
$ var="Fustigate"
$ echo "$var Programming"
$ echo '$var Programming'
Output:
Example-4: Declaring and reading numeric data using variable southward
One of the major limitations of Fustigate programming is that it can't perform arithmetics operations similar other programming languages. Numeric values are taken as strings in Fustigate. So no arithmetic operation tin can be washed by normal expression and it just combines the numeric values. If you write the expression with double first bracket then the arithmetics operation works properly. Run the following commands from the terminal.
$ north=100
$ repeat $n
$ echo $northward+xx
$ ( ( n=n+twenty ) )
$ repeat $north
Output:
Case-5: Doing arithmetic functioning using bc command
bc command is some other way to do arithmetics performance in Bash. Run the following commands from the terminal. When y'all use bc command merely for doing any arithmetic operation then partial parts are omitted from the result. You accept to utilise -50 choice with bc command to go the result with fractional value.
$ n=55
$ echo $n / 10 | bc
$ repeat $n / ten | bc -l
Output:
Using variables in fustigate file
You tin ascertain variable in bash file by the same way which are mentioned in above examples. You accept to create file with .sh or .bash extension to run bash script.
Example-half-dozen: Creating elementary fustigate script
Copy the post-obit code in a text editor and salve the file with fustigate extension. In this script, 1 cord and one numeric variables are declared.
str="Learn BASH Programming"
#print string value
repeat $str
num=120
#subtract twenty from numeric variable
( ( outcome=$num-20 ) )
#print numeric value
echo $effect
Output:
Example-seven: Using global and local variables
In the post-obit script, one global variable n and two local variables due north and m are used.
When the role addition() is called then the value of the local variable n is taken for adding merely global variable n remains unchanged.
#!/bin/bash
n=5
function addition( )
{
local n=6
local m=4
( ( n=n+m ) )
echo $north
}
addition
echo $n
Output:
Example-viii: Using array variable
Array variable is used to store a list of data. The following example shows how you apply of array variable in fustigate script. The elements of whatever assortment are separated by space in BASH. Here, an array of half-dozen elements is alleged. There is no built-in function or belongings to count the full elements of the array. # with * is used to count total elements. All elements are indicated by *. For loop is used here to iterate the array values. Reading array values and assortment values with key are shown in the next office of this script.
#!/bin/fustigate
myarr=(HTML JavaScript PHP jQuery AngularJS CodeIgniter)
#Count total number of elements of the array
total=${#myarr[*]}
repeat "Total elements: $total"
#Print each element value of the array
echo "Array values :"
for val in ${myarr[*]}
practice
printf " %due south\due north" $val
done
#Print each element value of the array with key
echo "Array values with key:"
for key in ${!myarr[*]}
do
printf "%4d: %s\n" $key ${myarr[$key]}
done
Output:
To use Fustigate variables properly you need a articulate concept on the declaration and apply of variables. This tutorial volition assistance you lot to get a clear idea on BASH variables. Afterward exercising the higher up examples properly yous will be able to use variables more efficiently in your bash scripts.
abadwhorknotho1940.blogspot.com
Source: https://linuxhint.com/variables-bash-programming/
0 Response to "Bash Read File Into Variavle Then Replace Variables"
Postar um comentário