Core Features of Bash
- Command History: Executed commands are stored in
~/.bash_history. This file contains records from previous sessions. Commands from the current active session are held in memory and written to the file upon successful logout. - Tab Completion:
- Command completion: Press
Tabafter the first word of a command string. - File completion: Press
Tabafter the second word or later. - Double-tapping
Tabat a prompt lists all available executable commands in the environment. - Installing
bash-completionextends this functionality to include options and parameters for specific commands.
- Command completion: Press
- Aliases: Shortcuts for command strings. For example,
alias lm='ls -al'allowslmto function identically tols -al. - Job Control: Menaging foreground and background processes.
- Shell Scripting: Writing executable program scripts.
- Wildcards: Pattern matching for filenames. For instance,
ls -l /usr/bin/X*lists files starting with 'X' in/usr/bin.
Inspecting Commands with type
type [-tpa] command_name
- Without options,
typeidentifies if a command is a shell builtin or an external binary. -t: Outputs a single word:file(external),alias, orbuiltin.-p: Displays the full path if the command is an external file.-a: Lists all occurrences of the command found inPATH, including aliases.
Executing Commands
To split a long command across multiple lines, use the backslash \ to escape the Enter key. The > symbol indicates the continuation prompt.
# Splitting a long copy command into two lines
[user@host ~]$ cp /var/spool/mail/root /etc/crontab \
> /etc/fstab /root
Useful Keyboard Shortcuts
| Key Combination | Action |
|---|---|
Ctrl + U |
Delete text from cursor to beginning of line |
Ctrl + K |
Delete text from cursor to end of line |
Ctrl + A |
Move cursor to the start of the line |
Ctrl + E |
Move cursor to the end of the line |
Managing Variables
Displaying and Setting Variables (echo, unset)
Rules for assignment:
- Use
=to assign values (e.g.,var=value). - No spaces allowed around the
=sign. - Variable names must start with a letter or underscore, followed by alphanumeric characters.
- Use
""for strings needing variable expansion (e.g.,$var). Use''for literal strings. - Escape special characters with
\. - Use backticks
`command`or$(command)for command substitution. - Append to a variable using
$varor${var}(e.g.,var=${var}:new_content). - Use
exportto make a variable available to child processes (environment variable). - System variables are typically UPPERCASE; user variables are lowercase.
- Remove a variable with
unset var_name.
Environment Variables (env, set, export)
env: Lists current environment variables.HOME: User's home directory.SHELL: Current shell path (e.g.,/bin/bash).PATH: Search paths for executables, separated by colons:.LANG: System language/locale settings.HISTSIZE: Number of history entries to keep.RANDOM: Generates random integers between 0 and 32767.
set: Displays all variables (including local and environment variables).export: Converts a local variable into an environment variable.
Customizing the Prompt (PS1):
You can modify the PS1 variable to change the prompt appearance.
| Code | Description |
|---|---|
\u |
Username |
\h |
Hostname (short) |
\H |
Hostname (full) |
\w |
Current working directory (full path) |
\W |
Current working directory (basename) |
\t |
Time (24-hour HH:MM:SS) |
\$ |
# for root, $ for others |
\! |
History number of the command |
Return Values:
$?: Holds the exit status of the last executed command (0 for success, non-zero for failure).$$: Process ID (PID) of the current shell.
Locales
locale -a: Lists all supported locales.locale: Shows current locale settings.LANGandLC_ALLare the primary variables controlling system language. System defaults are often configured in/etc/locale.conf.
Input, Arrays, and Declarations
Reading Input (read)
read [-pt] variable_name
-p: Display a prompt string.-t: Set a timeout in seconds.
Declaring Types (declare)
declare [-aixr] variable_name
- By default, variables are strings.
-a: Define as an array.-i: Define as an integer (math is limited to integer operations).-x: Export variable (same asexport).+x: Unexport variable.-r: Make variable read-only (cannot be unset or changed).
System Limits (ulimit)
ulimit [-SHacdfltu] [limit]
-S: Soft limit (warning threshold).-H: Hard limit (absolute maximum).-a: List all current limits.-f: Maximum file size (in blocks).-n: Max number of open file descriptors.-u: Max number of user processes.
String Manipulation
Deletion and Replacement
Assuming path="/usr/local/bin:/usr/bin:/bin":
| Syntax | Description |
|---|---|
${path#*:} |
Remove shortest match of *:* from front |
${path##*:} |
Remove longest match of *:* from front |
${path%:*} |
Remove shortest match of :* from back |
${path%%:*} |
Remove longest match of :* from back |
${path/bin/sbin} |
Replace first occurrence of bin with sbin |
${path//bin/sbin} |
Replace all occurrences of bin with sbin |
Default Values and Substitution
| Syntax | Behavior if var is unset |
Behavior if var is null |
Behavior if var is set |
|---|---|---|---|
${var:-default} |
Use default |
Use default |
Use var |
${var:=default} |
Set var to default and use it |
Set var to default and use it |
Use var |
${var:+alternate} |
Use nothing | Use nothing | Use alternate |
${var:?message} |
Print message to stderr |
Print message to stderr |
Use var |
Aliases and History
Managing Aliases
alias: List current aliases.alias hi='history | head': Create a new alias.unalias hi: Remove an alias.
History Commands
history [n] [-c] [-raw] [file]
n: Show lastncommands.-c: Clear history list.-a: Append current session history to file.-w: Write current history to file.
Execution shortcuts:
!n: Execute command numbernfrom history.!string: Execute the last command starting withstring.!!: Execute the previous command.
Shell Environment and Symbols
Common Control Keys
| Key Combination | Action |
|---|---|
Ctrl + C |
Interrupt/Send SIGINT to current process |
Ctrl + D |
End of File (EOF) / Logout |
Ctrl + Z |
Suspend current process (background) |
Ctrl + S |
Stop screen output |
Ctrl + Q |
Resume screen output |
Wildcards
| Symbol | Meaning |
|---|---|
* |
Matches 0 or more characters |
? |
Matches exactly one character |
[abc] |
Matches any single character inside brackets |
[a-z] |
Matches any single character in range |
[^abc] |
Matches any single character NOT inside brackets |
Special Characters
| Symbol | Meaning |
|---|---|
# |
Comment line |
\ |
Escape character |
| ` | ` |
; |
Command separator |
& |
Run command in background |
> |
Redierct output (overwrite) |
>> |
Redirect output (append) |
2> |
Redirect standard error |
$(...) |
Command substitution |
Piping and Text Processing
cut
Extracts specific sections of lines.
cut -d ':' -f 1,3 /etc/passwd: Use colon as delimiter, extract fields 1 and 3.cut -c 1-5 data.txt: Extract characters 1 through 5 from each line.
grep
Searches for patterns in files.
grep [-inv] 'pattern' file
-i: Ignore case.-n: Show line numbers.-v: Invert match (show non-matching lines).--color=auto: Highlight matched pattern.