-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableManipulationExample.txt
More file actions
118 lines (105 loc) · 3.46 KB
/
Copy pathVariableManipulationExample.txt
File metadata and controls
118 lines (105 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
So Variables, what are they?
Well they allow us to store contents into
a space and allows us to call upon them at a
later time
If you wanted to create a variable from your
terminal all you need to do is this
VAR="Im a Variable"
Note name and contents dont need to be the same
just decided to use this
now in order to extract that information we use
a $ followed with the variable name
Example:
$VAR
Other ways of doing it are
${VAR}
Reason you use the above method is to concatenate
multiple variables together.
So we will now run the command
>echo $VAR
>Im a Variable
System Used Variables
These are Variables that the system uses and should not be altered,
I will list off a few but to see a comprehensive list of all the system used variables
type "env" or "env | more" in case there are more to show.
UID - Contains the current user's UID
IFS - Input Field Seperator - Changing this will change the way
information will be parsed with certain commands, dont change this
unless you know what you are doing, IFS is <space><tab><\n>
HOSTNAME - the computers name
Position Variables
These variables are $0 - $99 they are basically your arguments to a command
For Example:
$0 is always your script filename - ./NoName.exe - $0
File:
1001001010001110
grep -o "1" File
-o is your first argument - $1
1 is your second - $2
File is your third -$3
Variables and what you can do with them
For the upcomming examples, in order to avoid confusion
We will be reading Left to Right
You can also be very selective with what parts of a variable
you want to see from the output
For Example
>STUFF=Variable.Stuff
>echo $STUFF
>Variable.Stuff
What the variable unchanged looks like
>echo ${STUFF%.*}
>Variable
Breakdown:
STUFF is the variable
% Signifies the last instance of the upcoming character and
shows everything to the left of that character
* wildcard, so show the rest of the variable
You can also do the same thing but to the right of the variable
For Example:
>STUFF=Variable.Stuff
>echo ${STUFF#*.}
>Stuff
Breakdown:
STUFF is the variable
# Signifies the first instance of the upcoming character and
shows everything to the right of that character
* Wildcard, so show the rest of the variable.
You are also able to further this method by adding an addition % or #
For Example:
>STUFF=Variable.Variable.Variable.Stuff
>echo ${STUFF##*.}
>Stuff
>echo ${STUFF%%.*}
>Variable
Breakdown:
## - Checks the very last instance of the character,
shows the rest of the variable to the right of the character
%% - checks the very first instance of the character and then
shows the rest of the variable to the left of the character
PT3 Variable Substitution
Incase you need to substitute certain contents of the variable you can!
Note: This will only substitute the first instance from Left to Right
For Example:
>STUFF="I would Like a Dog"
>echo ${STUFF/a/the}
Breakdown:
a - is the character(s) that you would like to substitute
the - is going to be the string of characters that will be
changed to
>I would Like the dog
>STUFF="abcdefghijklmnopqrstuvwxyz"
>echo ${STUFF/a/abc}
>abcbcdefghijklmnopqrstuvwxyz
>STUFF="How Are You?"
>echo ${STUFF// }
>HowAreYou?
PT3-1 Capitalization and Lowercase
In Bash4.0 and on they introduced bash substitution that allows captitalization
and lowercase, there are other methods used before Bash4.0 but however those use commands
In bash4.4:
For Example:
>STUFF="AaBbCc"
>echo ${STUFF^^}
>AABBCC
>echo ${STUFF,,}
>aabbcc