Bash & Shell Scripting
-
Linux
Linux Basics
Bash
ShellScripting
Overview
Command Line Shortcuts
File Globbing
- Globbing is a wildcard expansion.
*
matches zero or more characters.?
matches any single characters.[0-9]
matches a range of numbers.[abc]
matches any of the characters in the list.[^abc]
matches all except the characters in the list.- Predefined character classes can be used.
- The syntax for a character classes is
[:keyword:]
, where keyword can be - alpha, upper, lower, digit, alnum, punct, space.
Examples:
The Tab Key
- Type Tab to complete command lines
- For the command name, it will complete a command name.
- For an argument, it will complete a file name.
Examples:
/---------------------------------------------------------------------------------------\ | Input | 1st Tab | 2nd Tab | |---------------------------------------------------------------------------------------| | | | Display all commands | | ca | | List all commands that start with ca | | dat | date | | |---------------------------------------------------------------------------------------| | cat d | cat dove | | | cat m | cat myfile.txt | | | cat p | cat pe | List all possible file names | \---------------------------------------------------------------------------------------/
History
- Bash stores a history of commands you’ve entered.
- Use
history
command to see list of REMEMBERED commands.
/---------------------------------------------------------------------------------------\ | !! Repeat last command | | !char Repeat last command That started with char | | !num Repeat a command by its number in history output | | | | !-n Repeat a command entered n command back | | !?abc Repeat last command that contains (as opposed to ?started with?) abc | \---------------------------------------------------------------------------------------/
/---------------------------------------------------------------\ | UP DOWN Keys Scroll through previous commands | | Ctrl+r Reverse-i-search | \---------------------------------------------------------------/
To recall last argument from previous command
/-----------------------------------------------------------------------\ | Esc . The escape key followed by a period. | | Alt+. Hold down alt key while pressing the period. | | !$ Only valid for last argument | \-----------------------------------------------------------------------/
- Use
^old^new
to repeat the last command with old changed to new.
- You can ignore repeated duplicate commands and repeated lines that only differ in prepended spaces by running the following command below, or by adding it to your
.bashrc
file.
NOTE!: HISTCONTROL=ignorespace
will ignore just the commands that begin with a space.
Use HISTCONTROL=ignoreboth
if you also want to ignore duplicates.
Command Line Expansion
The Tilde
- Tilde (~) refers to home directory.
- Tilde (~) is useful in environment where home directories exist in non-standard locations.
Examples:
Command Substitution
- Use of the backquotes is called command substitution.
- An alternative syntax of backquotes is to place the command in parentheses preceded by a dollar sign
$()
.
Examples:
Brace Expansion
- Shorthand for printing repetitive strings.
Examples:
Bash Variables
- Variables are named values useful for storing data or commands output.
- Set with
VARIABLE=VALUE
- Referenced with
$VARIABLE
Examples:
NOTES!: Bash recognizes that you are trying to set a variable when it sees the pattern text=text
. Note that there can be no spaces on either side of the = or you will get an error.
Command Editing Tricks
/---------------------------------------------------------------\ | Ctrl+a Moves to beginning of the line | | Ctrl+e Moves to end of the line | | Ctrl+u Deletes to beginning of the line | | Ctrl+k Deletes to end of the line | | Ctrl+Arrow Moves left or right by word | \---------------------------------------------------------------/
Gnome Terminal
- Accessed via Applications -> Accessories -> Terminal
- Graphical Terminal supports multiple tabbed shells.
/-----------------------------------------------------------------------\ | Ctrl+Shift+t Creates a new tab | | Ctrl+Shift+c Copies selected text | | Ctrl+Shift+v Paste text | | Ctrl+PgUp/Pgdn Switches to next/previous tab | | Shift+PgUp/PgDn Scroll up and down a screen at a time | \-----------------------------------------------------------------------/
Scripting Basics
- A shell script is simply a text file containing a series of commands or statements to be executed.
- Shell scripts are useful for
- Automating commonly used commands.
- Performing a system administration and troubleshooting.
- Creating simple applications.
- Manipulation of text or files.
Creating Shell Script
- Comments start with a
#
- First line contains the Magic Shebang Sequence
#!
- This tells the operating system which interpreter to use in order to execute the script.
- Make the script executables
- Ensure that the script is located in a directory listed by the
PATH
environmental variable. - To do this enter the following command
- If the script is not in a directory listed in the
PATH
variable, either move the script to a directory that is (such as$HOME/bin
) or specify the absolute or relative path on the command line when executing the script
First Shell Script
Newsletter
Get updated when I create new content.
Unsubscribe whenever. Never any spam.