Master the Bash Shell

CORE BASH
Bash is a shell program. Shell program is typically an executable binary which takes in input command and translates those to system calls that can interact with Operating System API. Also, bash is not not the only kind of shell program. There are many variants to this:

  1. Thompson Shell - 1971,
  2. Bourne Shell (sh) - 1977,
  3. C shell (csh) - 1978,
  4. Bourne Again Shell (bash) - 1987, etc.

Bash is the most widely seen, used and available shell. However, it is still not unheard of to end up on servers that do not have bash!

Globbing: Regular expressions are patterns used to search for matching strings. Globs look similar and perform a similar function, but are not the same. That’s the key point to understand about globs vs regular expressions. For example: * -> zero or more characters, ? -> matches for any single character, [abd] -> matches for any character from a, b or d, [a-d] -> matches from any character between a to d.

Quoting: Single quotes and double quotes have different meanings in bash. Quoting changes the way bash can read the line, making it decide whether to take these characters and transform them into something else, or just leave them be. In other words, any expression inside the double quotes gets interpreted whilst no evaluation happens when single quote is used. There is no special meaning to any character in single quote.
Note: Globs are neither evaluated in double quotes nor in single quotes.

Dotfile: dot files are hidden files, It is used you want to have a file that sits alongside other files but that is generally ignored. For example, you might write some code that reformats a set of text files in a folder, but you don’t want to reformat a dotfile that contains information about what’s in those text files. You cannot use the command ls *: this command won't list hidden files instead we will have to use ls .*.

direct assignment VS export VS using .bash_profile direct assignment of variables are accessible within a shell and as soon as a new shell is opened such variables will no longer be accessible. An alternate for accessing variables across multiple bash within the same shell is to use export command. For example, export LAST='Singh'. This command will be ensure the presence within the same terminal session. Still if access is needed across different terminals, use the ~/.bash_profile(this file will change based on the version of shell being used). Write export LAST=Singh in .bahs_profile file. Since this file is loaded on every new terminal launch, therefore all the variables gets exported in every new terminal. Other interesting commands include: env which is used to display all the environment variables, compgen -v is used to print all the variables in the env. Although, the main use of compgen command is to display any autocomplete command in terminal including the variables, for example -v is used with this command to print all the variables.

Functions: Syntax of function is as shown below:
$function testfx {
>    echo $1
>    echo $2
>}
Scope of variables are same as how it would behave whilst you use these instructions over a shel without the method. However, on occasions where local variables are needed we can set those inside the function using the keyword local. When a key local is used before a variable, its scope is limited to the function and the variables will not impact the execution outside the method.

Builtins: Builtins are commands that come ‘built in’ to the bash shell program. Normally you can’t easily tell the difference between a builtin, a program or a function. The best way to distinguish is to type builtin and one of those next to it. For instance if there is a function that you wrote with the same name as a builtin, then the best way to call the builtin is by typing builtin followed by the bash command.

Aliases: Finally there are aliases. Aliases are strings that the shell takes and translates to whatever that string is aliased to. Aliases take precedence over builtins.

Redirects: Lets look at some examples, its simpler that way:
echo "contents of file1" > file1
The > 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. Here it creates a file called file1 and puts the echoed string into it.

Pipes: Lets look an example:
cat file1 | grep -c file 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: |. The resulting output of 1 indicates the number lines that matched the word file in the the file called file1. These are the numbered file descriptors, and the first three are assigned to the numbers 0,1 and 2. 0 is standard input, 1 is standard output, and 2 is standard error