# LPIC1 Notes

### Shells

* CSH
* TCSH
* KSH
* ZSH
* BSH
* BASH BASH is based on BSH and are the most popular.

BASH - based on BSH → Sometimes with symbolic link.

TCSH - based on csh

* Not a default shell on any Distro
* Similar to BASH

KSH - is designed to take the best features of BSH & CSH and improve/extend on them.

ZSH - takes evolution of shell even further then KSH, incorporating features from earlier Shells as well.

Symbolic Link to System’s default shell: /bin/sh

### Commands

exit - terminates shell logout - terminates only login shell.

CTRL + A - move to start of line&#x20;

CTRL + E - move to end of line

CTRL + B - Doubles as ←&#x20;

CTRL + F - Doubles as →

CTRL + N - Doubles as arrow down&#x20;

CTRL + P - Doubles as arrow up

Esc + B - Jumps back&#x20;

Esc + F - Jumps forward

CTRL + C - Cancel&#x20;

CTRL + L - Clear (or you can type “clear”)

BACKSPACE - Deletes Backwards DELETE - Deletes Forward

CTRL + K - Deletes everything from cursor forward.

CTRL + T - Takes characters before cursor and moves it forward.

Esc + T - Moves whole word forward Esc + U - Changes case (uppercase, lowercase) Esc + C - Converts letter under cursor to uppercase moving cursor to end of line.

CTRL (X+E) - Full editor from EMACS

{% hint style="info" %}
Note: EMACS is a Editing system like VI (VIM)
{% endhint %}

cd - Change the working directory

pwd - Display the working directory (prints on the screen where you at!)

echo - displays the text you have entered (typing “echo Hello” causes the system to display the string “Hello”) - used for scripting

exec - runs an external program that you specified (executes program)

{% hint style="info" %}
Note: The exec command special feature: Rather then creating a new process that runs alongside the shell, the new process replaces the shell, when the new process terminates, it is as if you terminated the shell.
{% endhint %}

time - shows how long subsequent commands take to execute. 3 Times are displayed:

1. Total execution time (aka REAL TIME)
2. User CPU time
3. System CPU time

{% hint style="info" %}
Example: time pwd - tells you how long the system took to execute the pwd command. Time displays after the full command terminates.
{% endhint %}

set - displays a wide variety of options relating to bash operations.

{% hint style="info" %}
Note: These options are formatted much like environment variables but are not the same things.
{% endhint %}

exit / logout - both terminate the shell.

{% hint style="info" %}
Note: exit - terminates any shell / logout - terminates only login shells (those are launched automatically when you initiate a text-mode login as opposed to those that run in xterm windows)
{% endhint %}

### Confusion over Internal and External Commands

When duplicate internal and external commands exist, they sometimes produce subtly different results or accept different options.

These might create problems sometimes: Consider the pwd command and symbolic links to directories. Suppose you create a symbolic to /bin within your home directory and then cd into that directory. You then want to know where you are. The pwd command that’s internal to bash will produce a different result from the external pwd command.

```bash
$ pwd
/home/master/binlink
$ /bin/pwd
/usr/bin
```

As you can see, bash’s internal pwd show the path via the symbolic link, whereas the external command shows the path to which the link points.

The path is a list of directories in which commands can be found. It is defined by the PATH Environment Variable.

When you type a command that’s not recognized by the shell as one of its internal commands, the shell checks its path to find a program by that name and execute it.

{% hint style="danger" %}
WARNING: The root account should normally have a shorter path than ordinary user accounts, typically, you will omit directories that store GUI and other user-oriented programs from root’s path in order to discourage the use of the root account for routine operations, minimizing the risk of security.
{% endhint %}

In case of both programs on the path and paths you type as part of the command, the program must be masked as executable. - Done via the executable bit that is stored within the file.

In order to adjust a program’s executable status use chmod

### Shell Command Tricks

Tab - Command completion; type part of command or filename; press tab; shell tries to fill in rest of command.

{% hint style="info" %}
Note: Double-tap tab key for available options.

Note: Popular Linux Shells (bash, tcsh) support file completion, older shells might not.
{% endhint %}

History - Keeps record of every command you type.

Bash History is stored in the .bash\_history file in your home directory.

history - brings up commands you typed (typically the last 500 lines)

history -c - clears history (which can be handy if you’ve recently typed commands you’d rather not have discovered by others, such as commands that include passwords)

{% hint style="info" %}
Note: you can type the number of the command and execute it from history: !210 to execute command 210
{% endhint %}

### Search

CTRL + R - Reverse Search → Type unique char for the command → Pressing CTRL + R searches through the commands containing the search.

{% hint style="info" %}
Note: Once in the search you can hold CTRL + R for backwards search and CTRL + S for forwards search. CTRL + G terminates the search, or use arrow keys
{% endhint %}

If you want to edit the command after you have found it with search, you can move within the line with CTRL + A to move at start of the line or CTRL + E to move at the end of the line.

To delete text press: CTRL + D or the DELETE KEY which deletes the character under the cursor. Where by pressing BACKSPACE KEY deletes the character to the left of the cursor.

In order to launch a full-fledged editor to edit commands you can use CTRL + X followed by CTRL + E. - These commands are the most useful ones supported by bash.

{% hint style="info" %}
Note: Bash attempts to launch the editor defined by the $FCEDIT or $EDITOR environment variable or Emacs as a last resort
{% endhint %}

### Exploring Shell Configuration

Shells are configured through files that hold configuration options in a plain-text format.

The bash configuration files are actually bash shell scripts.

Main user configuration files for BASH:

* \~/.bshrc
* \~/.profile

Main global configuration files for BASH:

* /etc/bash.bashrc
* /etc/profile

{% hint style="info" %}
Note: By editing these files you can change whatever needs changing, for instance you can add directories to the $PATH environment variable.
{% endhint %}

Be careful when making changes to configuration files, particularly the global configuration file. Best practice: Make backup of original, test changes, if problem occurs revert back to original config file.

### Using Environment Variables

Environment variables are like variables in programming languages, they hold data to be referred to by the variable name.

You can add variables with the commands:

\= export

{% hint style="info" %}
Example:

ABC=google.com Export ABC

(This can also be done by using a single command)

export =

Example:

export ABC=google.com
{% endhint %}

There are programs that need this information and can refer to the environment variable:

Can be done from shell by using: echo $Variable

{% hint style="info" %}
Example:

echo $ABC google.com

env - view the entire variable environment

unset - delete an environment variable

Example: unset ABC
{% endhint %}

### Getting Help (Manual Pages)

man = manual; pages that provide summaries of what commands, features or files do.

Spacebar = move forward a page

Esc + V = move backward a page

/ = search for text

q = exit

info = info pages; are like man pages bu use hypertext format so you can move from section to section of the documentation for a program.

Good [link ](http://tldp.org)for research

### Using Streams, Redirection and Pipes

Streams, redirection and pipes are some of the more powerful command-line tools in Linux.

Linux treats input and output as streams, which is data that can be manipulated. You can manipulate the input or output ti come from/ go to other sources. Similarly you can pipe the output of one program into another.

### Exploring Types of Streams

Standard Input = Programs accept keyboard input via standard input aka STDIN. In most cases this is data that comes into computer from a keyboard.

Standard Out = Text-mode programs send most data to their users via standard output aka STDOUT, which is displayed on the screen.

Standard Error = Linux provides a second type of output stream known as standard error aka STDERR. This output stream is intended to carry high-priority info such as error messages.

{% hint style="info" %}
Note: Internally programs treat these streams just like data files. They open them, read from or write to the files, close them when they are done.
{% endhint %}

### Redirecting Input and Output

To redirect output you use symbols following the command including options it takes.

{% hint style="info" %}
Example:

echo $SHELL > shell.txt → result is that the shell.txt contains the output of the command in the @SHELL environment variable.
{% endhint %}

### Commands for Streams, Redirects and Pipes

\| = Creates a new file containing standard output. If file exists it will overwrite onto it.

|| = Appends standard output to the existing file. If file doesn’t exist, it creates it.

2> = Creates a new file containing standard error. If file exists, it overwrites it.

2>> = Appends standard error to the existing file. If the file doesn’t exist, it creates it.

&> = Creates new file containing both STDOUT and STDERR. If file exists, it overwrites it.

&>> = Appends STDOUT and STDERR to existing file. If file doesn’t exist, it creates is.

< = Sends the contents of the specific file to be used as standard input.

<< = Accepts text on the following lines as standard input.

<> = Causes the specific file to be used for both standard input and standard output.

\| = Redirects STDOUT of one program to STDIN of a second program.

{% hint style="info" %}
Note: The most important input redirection is < which takes the specified file content as standard input.
{% endhint %}

A common trick is to redirect STDOUT or STDERR to /dev/null - This file is a device that’s connected to nothing. It is used when you want to get rid of data.

tee = Command splits standard input so that it’s displayed on standard output and on as many files as you specify.

### Piping Data Between Programs

Programs can frequently operate on other programs’ output. You can use one programs STDOUT as another program's’ STDIN

#### How it works:

Normally if you use redirection you will have to: Send first program’s STDOUT to a file → redirect 2nd program’s STDIN to read from that file.

By doing this it can lead to unnecessary cluttering on your system.

Solution is to use pipes (aka pipelines):

A pipe redirects the first program’s STDOUT to the second program’s STDIN.

Generating Command Lines:

xargs = command that builds a command from its standard input.

#### Example:

printf “1\n2\n3\n” = would print lines 1, 2, 3

printf “1\n2\n3\n” | xargs touch = would create file 1, 2, 3

printf “1\n2\n3\n” | xargs - i touch {}.txt = would replace text for 1 2, 3 with the extension text. (The brackets {} shows the xargs command where to replace the text)

[Useful xargs video](https://www.youtube.com/watch?v=-yJTjrv0mYU)

Another xargs [intro](https://www.youtube.com/watch?v=8kAB_VgokMY)
