Skip to content

Commit 8b10d96

Browse files
variable related thing added
0 parents  commit 8b10d96

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

basics/01-variable1.bash

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
3+
# AUTHOR : Arnob kumar saha
4+
# Date created : 06-11-2021
5+
# Lase modified : 06-11-2021
6+
7+
# DESCRIPTION : This script will show some basic stuffs we can do with variables.
8+
# USAGE : ./01-variable.bash
9+
10+
11+
# no need do declare like, x="hello" if it doesn't contain any special character
12+
x=hello
13+
y=world
14+
15+
# $ is used to extract the value of a variable
16+
# Note : $var & ${var} are almost same. second style used to remove ambiguity.
17+
# The difference between these two is, ${var} is more flexible & we can do some stuffs (has been shown in 03-variable2.bash) ,
18+
# which we could not do uisng $var style.
19+
: '
20+
for example ,
21+
v=foo
22+
echo $vbar
23+
# Prints nothing because there is no variable 'vbar'
24+
echo ${v}bar
25+
# prints 'foobar'
26+
'
27+
echo "$x $y"
28+
29+
30+

basics/02-arithmatic.bash

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# AUTHOR : Arnob kumar saha
4+
# Date created : 06-11-2021
5+
# Lase modified : 06-11-2021
6+
7+
# DESCRIPTION : This script will show some way to evaluate arithmetic operations: $(( )), let, expr
8+
# USAGE : 02-./arithmatic.bash
9+
10+
11+
# first way
12+
n1=5
13+
n2=8
14+
15+
: '
16+
$(( EXPRESSION )) style is used for evaluating some arithmatic expression
17+
inside $(( )), using $ before the varibleName & not using $, is exactly same.
18+
'
19+
echo $(($n1+$n2))
20+
21+
echo $((n1+n2))
22+
23+
24+
25+
# second way
26+
: '
27+
We can use "let <arithmetic expression>" to evalutae expression also
28+
'
29+
30+
let "a = 5 + 4"
31+
let a++
32+
echo "a = $a"
33+
34+
let "4 + 5" # does nothing
35+
36+
37+
38+
# third way
39+
: '
40+
expr is similar to let except instead of saving the result to a variable it instead prints the answer.
41+
Unlike let you dont need to enclose the expression in quotes. You also must have spaces between
42+
the items of the expression. It is also common to use expr within command substitution to save the output to a variable.
43+
'
44+
expr 5 + 4
45+
expr "5 + 4"
46+
expr 5+4
47+
48+
expr 5 \* 3 # * is special character, so We need to escape its special meaning.
49+
a=$( expr 10 - 3 )
50+
echo $a

basics/03-variable2.bash

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
# AUTHOR : Arnob kumar saha
4+
# Date created : 06-11-2021
5+
# Lase modified : 06-11-2021
6+
7+
# DESCRIPTION : This script will show some interesting stuffs we can do with variables.
8+
# USAGE : ./03-variable.bash
9+
10+
11+
12+
# there is a special character space, so using double quote is needed.
13+
var="heLlo WoRld"
14+
15+
echo ${var,} # 1st letter to lowercase
16+
echo ${var,,} # all letters to lowercase
17+
echo ${var^} # 1st letter to uppercase
18+
echo ${var^^} # all letters to uppercase
19+
echo ${#var} # length
20+
21+
echo ${var::5} # first 5 characters
22+
echo ${var:4} # all excluding first 4 characters
23+
echo ${var: -3} # last 3 characters, space before minus is required.
24+
echo ${var:2:5} # staring from 2nd letter, cut a srring of length 5.
25+
26+
27+
echo "-------------------------"
28+
29+
: '
30+
We have seen when to use ${var} style over $var.
31+
Now we will see the difference between $var, "$var" & "${var}"
32+
When using quoting "" , we tell the bash to remove the special meaning of all the special characters except $ and `(backtick)
33+
34+
'
35+
36+
var="foo bar"
37+
touch $var # expands to touch foo bar. will create two files named foo & bar.
38+
touch "$var" # expands to touch "foo bar". will create a single file name "foo bar".
39+
40+
: '
41+
"${var}" is used for removing ambiguity of "$var" , just like ${var} does so for $var.
42+
'
43+
44+
echo "$varxyz" # prints nothing
45+
echo "${var}xyz" # prints.. foo barxyz

0 commit comments

Comments
 (0)