Skip to content

Commit f46d0e5

Browse files
paramter expansion
1 parent 5426f86 commit f46d0e5

File tree

4 files changed

+175
-2
lines changed

4 files changed

+175
-2
lines changed

basics/03-variable2.bash

+5-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ echo ${var^} # 1st letter to uppercase
1818
echo ${var^^} # all letters to uppercase
1919
echo ${#var} # length
2020

21+
echo "*****"
2122
echo ${var::5} # first 5 characters
2223
echo ${var:4} # all excluding first 4 characters
2324
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-
25+
echo ${var:2:5} # staring from 2nd letter, cut a string of length 5.
26+
echo ${var:2: -3} # strating from 2nd letter, not including last 3 letter
27+
echo ${var: -4:3} # takes last 4 letters, & then take first 3 of that
28+
echo ${var: -4: -3} # takes last 4 letters, & then skip last 3 of that
2629

2730
echo "-------------------------"
2831

how-bash-process/parameter.bash

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/bin/bash
2+
3+
# AUTHOR : Arnob kumar saha
4+
# Date created : 19-01-2022
5+
# Lase modified : 20-01-2022
6+
7+
# DESCRIPTION : describes shell parameter expansion with examples
8+
# USAGE : ./parameter.bash
9+
10+
: '
11+
There are total of GROUPs :
12+
1. ${parameter:-word}
13+
${parameter-word}
14+
2. ${parameter:=word}
15+
${parameter=word}
16+
3. ${parameter:?word}
17+
${parameter?word}
18+
4. ${parameter:+word}
19+
${parameter+word}
20+
'
21+
22+
: '
23+
Parameters are three types :
24+
1) Set & not null
25+
2) set & null [[colon(:) only makes impact in this case]]
26+
3) unset (not declared)
27+
'
28+
29+
: '
30+
#CASE 1 (-) : If unset, use value
31+
# Parameter is set and not null
32+
VAR_1=var1
33+
echo "VAR_1 is ${VAR_1:-abc}" # "var1"
34+
# Parameter is set but null
35+
VAR_2=
36+
echo "VAR_2 is ${VAR_2:-abc}" # "abc"
37+
echo "This will not hold the value $VAR_2." # ""
38+
# Parameter is not set
39+
echo "VAR_3 is ${VAR_3:-abc}" # "abc"
40+
41+
# now you will see, :- makes impact for VAR_2 where - doesnot
42+
VAR_2=
43+
echo "VAR_2 is ${VAR_2-abc}" # ""
44+
'
45+
46+
47+
: '
48+
#CASE 2 (=) : If unset, set value
49+
# Parameter is set and not null
50+
VAR_1="var 1"
51+
echo "VAR_1 is ${VAR_1:=abc}."
52+
# Parameter is set but null
53+
VAR_2=
54+
echo "VAR_2 is ${VAR_2:=abc}."
55+
echo "The VAR_2 holds the value $VAR_2." # "abc"
56+
# Parameter is not set
57+
echo "VAR_3 is ${VAR_3:=abc}."
58+
59+
VAR_2=
60+
echo "VAR_2 is ${VAR_2=abc}"
61+
'
62+
63+
64+
65+
: '
66+
#CASE 3 (+) : If set, use value
67+
VAR_1=var1
68+
echo "VAR_1 is ${VAR_1:+new var}" # "new var"
69+
echo "VAR_1 is $VAR_1." # "var1"
70+
# null value, no substitution
71+
VAR_2=
72+
echo "VAR_2 is ${VAR_2:+new var}" # ""
73+
# unset, no substitution
74+
echo "VAR_3 is ${VAR_3:+new var}" # ""
75+
76+
# null vaule, substitution
77+
VAR_2=
78+
echo "VAR_2 is ${VAR_2+abc}" # "abc"
79+
'
80+
81+
82+
: '
83+
#CASE 4 (?) : If set, use value. else give error-value in the standard error
84+
# Parameter is set and not null
85+
VAR_1="var 1"
86+
echo "VAR_1 is ${VAR_1:?no value}."
87+
# Parameter is set but null
88+
VAR_2=
89+
echo "VAR_2 is ${VAR_2:?no value}." # provides error./bash 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
90+
echo "The VAR_2 holds the value $VAR_2."
91+
# Parameter is not set
92+
echo "VAR_3 is ${VAR_3:?no value}." # provides error
93+
echo "The VAR_3 holds the value $VAR_3."
94+
95+
# Parameter is set but null
96+
VAR_2=
97+
echo "VAR_2 is ${VAR_2?no value}" # ""
98+
'
99+
100+
101+
# SetAndNotNull SetButNull Unset
102+
# ${parameter:-word} substitute parameter substitute word substitute word
103+
# ${parameter-word} substitute parameter substitute null substitute word
104+
# ${parameter:=word} substitute parameter assign word assign word
105+
# ${parameter=word} substitute parameter substitute null assign word
106+
# ${parameter:+word} substitute word substitute null substitute null
107+
# ${parameter+word} substitute word substitute word substitute null
108+
# ${parameter:?word} substitute parameter error, exit error, exit
109+
# ${parameter?word} substitute parameter substitute null error, exit
110+
111+
# If the parameter is SetButNull, -,=,? takes the null value.
112+
# Whereas, :-.:=,;? takes the word value.
113+
# + is exactly the opposite.
114+

how-bash-process/parameter2.bash

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# AUTHOR : Arnob kumar saha
4+
# Date created : 19-01-2022
5+
# Lase modified : 20-01-2022
6+
7+
# DESCRIPTION : describes shell parameter expansion with examples
8+
# USAGE : ./parameter2.bash
9+
10+
11+
# all the examples from basics/03-variable.bash are of parameter expansion.
12+
13+
: '
14+
set -- 1 2 3 4 5 6 7 8 9 0 a b c d e f g h
15+
16+
echo ${@} # list of all paramters, note that ${0} has not been echoed.
17+
# to do so, use this, echo ${@:0}
18+
echo ${0} # first positional parameter
19+
20+
echo ${@:7} # all positional params starting from index 7
21+
echo ${@:7:2} # 2 param starting from index 7
22+
23+
echo ${@:7:-2} #bash: -2: substring expression < 0
24+
echo ${@: -7:2} # -1 is the last positional paramter. -7 is 7th from the last, then take 2 items
25+
#b c
26+
echo ${@:0:2}
27+
28+
# We could run these commands for array also .
29+
array=(1 2 3 4 5 6 7 8 9 10)
30+
echo ${array[@]: -7:2} $ prints 4 5
31+
32+
echo ${array[5]}
33+
'
34+
35+
VAR1="VAR1"
36+
VAR2="VAR2"
37+
NON_VAR="VAR3"
38+
39+
echo "${!VAR*}"
40+
echo "${!VAR@}"
41+
echo "${!^*}"
42+
43+
#name=(3 5 7)
44+
name=
45+
echo ${!name[@]}
46+
# if name is array , it prints 0 1 2
47+
# else : if name is set (null or not null), prints 0
48+
#if name is not declared, prints nothing

how-bash-process/readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ mkdir -p path/{ucb/{ex,edit},lib/{ext,abc}} # make directories .. <br>path/lib/
4646

4747
### b) Intermidiate Expansion :
4848

49+
In this step, different types expansions & substitutions occur. Namely, <br>
50+
i) Tilde Expansion : ~ converts to $HOME. For example, ~arnob/Documents converts to /home/arnob/Documents. <br>
51+
52+
ii) Parameter expansion : Discussed in parameter.bash & paramter2.bash files. <br>
53+
iii) Arithmatic expansion : <br>
54+
iv) Command Substitution : <br>
55+
v) Process substitution :
56+
4957
### c) Word splitting :
5058

5159
### d) Globbing :

0 commit comments

Comments
 (0)