Shell types

A comprehensive guide to the Linux shell environment, including configuration, customization, and best practices.

🌐 Shell Basics

Shell Types

# View available shells
cat /etc/shells       # List all shells
echo $SHELL          # Current shell
chsh -s /bin/bash    # Change default shell

# Common shells
/bin/bash            # Bourne Again Shell
/bin/zsh             # Z Shell
/bin/fish            # Friendly Interactive Shell
/bin/dash            # Debian Almquist Shell

Shell Environment

# Environment information
env                  # Display environment variables
printenv             # Print environment variables
set                  # Show all variables
export               # Export variable to environment

πŸ”§ Shell Configuration

Bash Configuration Files

# Global configuration
/etc/profile         # System-wide settings
/etc/bash.bashrc     # System-wide bash settings
/etc/profile.d/*.sh  # Additional settings

# User configuration
~/.profile           # User profile
~/.bashrc            # User bash settings
~/.bash_profile      # Login shell settings
~/.bash_logout       # Logout settings

Environment Variables

# Common variables
$HOME               # User's home directory
$PATH               # Command search path
$PS1                # Primary prompt string
$PS2                # Secondary prompt string
$SHELL              # Current shell
$USER               # Current username
$PWD                # Current directory
$HOSTNAME           # System hostname

🎨 Shell Customization

Prompt Customization

# PS1 elements
\u                  # Username
\h                  # Hostname
\w                  # Current directory
\W                  # Base of current directory
\d                  # Date
\t                  # Time
\$                  # $ for user, # for root

# Color codes
\e[30m              # Black text
\e[31m              # Red text
\e[32m              # Green text
\e[0m               # Reset color

Aliases

# Alias management
alias               # List all aliases
alias ls='ls --color=auto'  # Add color to ls
alias ll='ls -la'   # Long listing format
unalias name        # Remove alias

# Persistent aliases
# Add to ~/.bashrc:
alias update='sudo apt update && sudo apt upgrade'

πŸ“ File System Navigation

Directory Navigation

# Navigation shortcuts
cd -                # Previous directory
cd ~                # Home directory
cd                  # Home directory
pushd dir           # Push directory to stack
popd                # Pop directory from stack
dirs                # Show directory stack

Path Management

# Path manipulation
PATH=$PATH:/new/path  # Add to PATH
CDPATH=.:~:/etc      # CD search path
MANPATH              # Manual page path

πŸ” Command History

History Management

# History commands
history             # Show command history
!n                  # Execute command n
!!                  # Execute last command
!string             # Execute last command starting with string
!$                  # Last argument of previous command
!*                  # All arguments of previous command

# History settings
HISTSIZE=1000       # History size in memory
HISTFILESIZE=2000   # History size in file
HISTCONTROL=ignoredups  # Ignore duplicates

History Shortcuts

ctrl+r              # Reverse history search
ctrl+s              # Forward history search
ctrl+g              # Cancel search

⌨️ Command Line Editing

Readline Shortcuts

# Cursor movement
ctrl+a              # Start of line
ctrl+e              # End of line
alt+b               # Back one word
alt+f               # Forward one word

# Text manipulation
ctrl+u              # Cut to start of line
ctrl+k              # Cut to end of line
ctrl+w              # Cut previous word
ctrl+y              # Paste cut text

Command Completion

tab                 # Auto-complete command/filename
tab tab             # Show all possibilities
alt+/               # Complete filename
alt+~               # Complete username

πŸ”„ Job Control

Background Jobs

# Job management
command &           # Run in background
ctrl+z              # Suspend current job
bg                  # Resume in background
fg                  # Resume in foreground
jobs                # List all jobs

Process Management

# Process control
kill %n             # Kill job number n
wait %n             # Wait for job n
disown %n           # Disown job n
nohup command &     # Run immune to hangups

πŸ› οΈ Advanced Features

Command Substitution

# Command substitution
$(command)          # New style
`command`           # Old style
result=$(ls -l)     # Store command output

Parameter Expansion

# Variable manipulation
${var:-default}     # Use default if unset
${var:=default}     # Set default if unset
${var:?message}     # Error if unset
${var:+value}       # Use value if set
${#var}             # String length
${var#pattern}      # Remove shortest match from start
${var##pattern}     # Remove longest match from start
${var%pattern}      # Remove shortest match from end
${var%%pattern}     # Remove longest match from end

πŸ“Š Input/Output Control

Redirection

# File redirection
command > file      # Output to file
command >> file     # Append to file
command < file      # Input from file
command 2> file     # Error to file
command &> file     # Output and error to file

# Descriptors
0                   # Standard input
1                   # Standard output
2                   # Standard error

Pipes and Filters

# Pipeline operations
command1 | command2  # Pipe output
command1 |& command2 # Pipe output and error
tee file            # Split output stream

πŸ’‘ Best Practices

  1. Shell Configuration

    • Keep configuration organized

    • Document custom settings

    • Use modular configuration

    • Backup important files

  2. Environment Management

    • Use meaningful variable names

    • Set appropriate permissions

    • Clean up environment

    • Use local variables

  3. Command Line Efficiency

    • Use aliases for common commands

    • Master keyboard shortcuts

    • Utilize command history

    • Learn pattern matching

  4. Security

    • Avoid storing sensitive data

    • Use appropriate permissions

    • Regular security audits

    • Clean history regularly

πŸ”§ Troubleshooting

Common Issues

# Shell problems
echo $?              # Last command status
set -x               # Debug mode on
set +x               # Debug mode off
bash -x script.sh    # Debug script

# Environment issues
env -i bash          # Clean environment
printenv | sort      # Check variables
which command        # Find command location

Performance

# Shell optimization
set -o               # View shell options
shopt               # Shell options
ulimit -a           # Resource limits
time command        # Time command execution

πŸ“š Additional Resources

  1. Documentation

    • man bash

    • info bash

    • help command

    • /usr/share/doc/bash

  2. Configuration Examples

    • Sample .bashrc

    • Sample .profile

    • Shell scripts

    • Utility functions

  3. Learning Resources

    • Online tutorials

    • Shell scripting guides

    • Command references

    • Best practices guides

Last updated

Was this helpful?