You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h5class="w3-center w3-padding-64"><spanclass="w3-tag w3-wide">Master the Bash Shell</span></h5>
50
+
</p>
51
+
52
+
<pclass="w3-content" style="max-width:900px">
53
+
<b>CORE BASH</b><br>
54
+
Bash is a shell program. Shell program is typically an executable binary which takes in input command and
55
+
translates those to system calls that can interact with Operating System API. Also, <it>bash</it> is not
56
+
not the only kind of shell program. There are many variants to this:
57
+
<olclass="w3-content" style="max-width:900px">
58
+
<li>Thompson Shell - 1971,</li>
59
+
<li>Bourne Shell (sh) - 1977,</li>
60
+
<li>C shell (csh) - 1978, </li>
61
+
<li>Bourne Again Shell (bash) - 1987, etc.</li>
62
+
</ol>
63
+
<class="w3-content" style="max-width:900px">
64
+
Bash is the most widely seen, used and available shell. However, it is still not unheard of to end up on
65
+
servers that do not have bash!<br><br>
66
+
<b>Globbing: </b>
67
+
Regular expressions are patterns used to search for matching strings. Globs look similar and perform a
68
+
similar function, but are not the same. That’s the key point to understand about globs vs regular expressions.
69
+
For example: * -> zero or more characters, ? -> matches for any single character, [abd] -> matches for any character from a, b or d,
70
+
[a-d] -> matches from any character between a to d.
71
+
<br><br>
72
+
<b>Quoting: </b> Single quotes and double quotes have different meanings in bash.
73
+
Quoting changes the way bash can read the line, making it decide whether to take these characters and
74
+
transform them into something else, or just leave them be. In other words, any expression inside the double
75
+
quotes gets interpreted whilst no evaluation happens when single quote is used. There is no special meaning
76
+
to any character in single quote.<br>
77
+
Note: Globs are neither evaluated in double quotes nor in single quotes.<br><br>
78
+
<b>Dotfile: </b> dot files are hidden files, It is used you want to have a file that sits
79
+
alongside other files but that is generally ignored. For example, you might write some code that
80
+
reformats a set of text files in a folder, but you don’t want to reformat a dotfile that contains
81
+
information about what’s in those text files. You cannot use the command <em>ls *</em>: this command
82
+
won't list hidden files instead we will have to use <em>ls .*</em>.<br><br>
83
+
<b>direct assignment VS export VS using .bash_profile</b> direct assignment of variables are accessible within
84
+
a shell and as soon as a new shell is opened such variables will no longer be accessible. An alternate for
85
+
accessing variables across multiple bash within the same shell is to use <em>export</em> command.
86
+
For example, export LAST='Singh'. This command will be ensure the presence within the same terminal session.
87
+
Still if access is needed across different terminals, use the <em>~/.bash_profile</em>(this file will
88
+
change based on the version of shell being used). Write <em>export LAST=Singh</em> in .bahs_profile file.
89
+
Since this file is loaded on every new terminal launch, therefore all the variables gets exported in every new terminal.
90
+
Other interesting commands include: <b>env</b> which is used to display all the environment variables,
91
+
<b>compgen -v</b> is used to print all the variables in the env. Although, the main use of compgen command is to
92
+
display any autocomplete command in terminal including the variables, for example <em>-v</em> is used with this command to print all
93
+
the variables.
94
+
<br><br>
95
+
<b>Functions: </b> Syntax of function is as shown below: <br>
96
+
$function testfx {<br>
97
+
> echo $1<br>
98
+
> echo $2<br>
99
+
>}<br>
100
+
Scope of variables are same as how it would behave whilst you use these instructions over a shel without the
101
+
method. However, on occasions where local variables are needed we can set those inside the function using the
102
+
keyword local. When a key local is used before a variable, its scope is limited to the function and the variables
103
+
will not impact the execution outside the method. <br><br>
104
+
<b>Builtins: </b> Builtins are commands that come ‘built in’ to the bash shell program. Normally you
105
+
can’t easily tell the difference between a builtin, a program or a function. The best way to distinguish
106
+
is to type builtin and one of those next to it. For instance if there is a function that you wrote with
107
+
the same name as a builtin, then the best way to call the builtin is by typing builtin followed by the
108
+
bash command.
109
+
<br><br>
110
+
<b>Aliases: </b> Finally there are aliases. Aliases are strings that the shell takes and translates to whatever
111
+
that string is aliased to. Aliases take precedence over builtins.<br><br>
112
+
<b>Redirects: </b> Lets look at some examples, its simpler that way: <br>
113
+
<em>echo "contents of file1" > file1</em><br>
114
+
The <em>></em> character is the redirect operator. This takes the output from the preceding command that you’d normally see in the terminal and sends it to a file that you give it.
115
+
Here it creates a file called file1 and puts the echoed string into it.<br><br>
116
+
<b>Pipes: </b> Lets look an example:<br>
117
+
<em>cat file1 | grep -c file</em>
118
+
Normally you’d run a grep with the filename as the last argument, but instead here we pipe the contents of file1 into the grep command by using the ‘pipe’ operator: |.
119
+
The resulting output of 1 indicates the number lines that matched the word file in the the file called file1.
120
+
These are the numbered file descriptors, and the first three are assigned to the numbers 0,1 and 2.
0 commit comments