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 ShellShell 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 settingsEnvironment 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 colorAliases
# 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 stackPath 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 duplicatesHistory 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 textCommand 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 jobsProcess 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 outputParameter 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 errorPipes and Filters
# Pipeline operations
command1 | command2 # Pipe output
command1 |& command2 # Pipe output and error
tee file # Split output streamπ‘ Best Practices
Shell Configuration
Keep configuration organized
Document custom settings
Use modular configuration
Backup important files
Environment Management
Use meaningful variable names
Set appropriate permissions
Clean up environment
Use local variables
Command Line Efficiency
Use aliases for common commands
Master keyboard shortcuts
Utilize command history
Learn pattern matching
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 locationPerformance
# Shell optimization
set -o # View shell options
shopt # Shell options
ulimit -a # Resource limits
time command # Time command executionπ Additional Resources
Documentation
man bash
info bash
help command
/usr/share/doc/bash
Configuration Examples
Sample .bashrc
Sample .profile
Shell scripts
Utility functions
Learning Resources
Online tutorials
Shell scripting guides
Command references
Best practices guides
Last updated
Was this helpful?