# Check if Bash is installedwhichbash
/bin/bash
# Check Bash versionbash--version
GNUbash,version5.1.16(1)-release(x86_64-pc-linux-gnu)# Check current shellecho$SHELL/bin/bash
# Check available shellscat/etc/shells
```# Bash - Bourne Again Shell<divclass="cheat-sheet-actions">
```bash
# Set Bash as default shell for current userchsh-s/bin/bash
# Set Bash as default shell for specific user (as root)sudochsh-s/bin/bashusername
# Verify shell changeecho$SHELL
pkg install bash
Bash (Bourne Again Shell) es un intérprete de comandos Unix y un lenguaje de comandos escrito por Brian Fox para el Proyecto GNU como un reemplazo de software libre para el Bourne Shell. Lanzado por primera vez en 1989, Bash se ha convertido en el intérprete predeterminado en la mayoría de las distribuciones Linux y es uno de los intérpretes más utilizados en el ecosistema Unix/Linux. Combina las características del shell Bourne original con funcionalidades adicionales, incluyendo completado de comandos, historial de comandos y capacidades de scripting mejoradas.bash
echo $0 # Script name
echo $1 # First argument
echo $# # Number of arguments
echo $@ # All arguments
echo $ # Process ID
echo $? # Exit status of last command
### Establecer Bash como Shell Predeterminadobash
ls
ls -l # Long format
ls -la # Long format with hidden files
ls -lh # Human readable sizes
ls -lt # Sort by modification time
ls -lS # Sort by size
ls -lR # Recursive listing
ls -la --color=auto # Colored output
ls -la --time-style=full-iso # ISO time format
ls -la --group-directories-first # Directories first
### Sustitución de Comandosbash
grep -A 3 "pattern" file.txt # Show 3 lines after match
grep -B 3 "pattern" file.txt # Show 3 lines before match
grep -C 3 "pattern" file.txt # Show 3 lines around match
grep -l "pattern" *.txt # Show only filenames
grep -c "pattern" file.txt # Count matches
### Operaciones de Archivosbash
sed 's/old/new/' file.txt # Replace first occurrence
sed 's/old/new/g' file.txt # Replace all occurrences
sed '1,5s/old/new/g' file.txt # Replace in lines 1-5
sed '/pattern/d' file.txt # Delete lines matching pattern
sed -n '1,10p' file.txt # Print lines 1-10
ls -l|grep "txt" # List files and filter for .txt
ps aux|grep "process_name" # Show processes and filter
cat file.txt|sort|uniq # Sort and remove duplicates
ps # Show current processes
ps aux # Show all processes
ps -ef # Full format listing
pstree # Show process tree
top # Real-time process monitor
htop # Enhanced process monitor
kill PID # Terminate process
kill -9 PID # Force kill process
killall process_name # Kill all processes by name
pkill pattern # Kill processes matching pattern
nohup command & # Run command immune to hangups
```## Redirección de Entrada/Salida y Tuberías
echo $\\{fruits[0]\\} # First element
echo $\\{fruits[@]\\} # All elements
echo $\\{#fruits[@]\\} # Array length
fruits+=("grape") # Append element
### Declaraciones Condicionalesbash
if [ "\(var" = "value" ]; then # String equality
if [ "\)num" -eq 10 ]; then # Numeric equality
if [ "\(num" -gt 5 ]; then # Greater than
if [ "\)num" -lt 20 ]; then # Less than
if [ -f "file.txt" ]; then # File exists
if [ -d "directory" ]; then # Directory exists
if [ -r "file.txt" ]; then # File is readable
if [ -w "file.txt" ]; then # File is writable
if [ -x "script.sh" ]; then # File is executable
echo $\\{variable\\}
echo $\\{variable:-default\\} # Use default if unset
echo $\\{variable:=default\\} # Set default if unset
echo $\\{variable:+alternate\\} # Use alternate if set
echo $\\{variable:?error\\} # Error if unset
string="Hello, World!"
echo $\\{string#Hello\\} # Remove shortest match from beginning
echo $\\{string##/\\} # Remove longest match from beginning
echo $\\{string%World!\\} # Remove shortest match from end
echo $\\{string%%/\\} # Remove longest match from end
echo $\\{string/Hello/Hi\\} # Replace first occurrence
echo $\\{string//l/L\\} # Replace all occurrences
echo $\\{string:0:5\\} # Extract substring (position:length)
echo $\\{string:7\\} # Extract from position to end
echo $\\{#string\\} # String length
### Expansión de Parámetrosbash
extract() \\{
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar e $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;
*.tgz) tar xzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress \(1 ;;
*.7z) 7z x \(1 ;;
*) echo "'\)1' cannot be extracted via extract()" ;;
esac
else
echo "'\)1' is not a valid file"
fi
\\}
set -o vi # Vi editing mode
set -o emacs # Emacs editing mode (default)
set -o noclobber # Prevent file overwriting
set +o noclobber # Allow file overwriting
Ctrl+A # Beginning of line
Ctrl+E # End of line
Ctrl+B # Back one character
Ctrl+F # Forward one character
Alt+B # Back one word
Alt+F # Forward one word
Ctrl+D # Delete character
Ctrl+H # Backspace
Ctrl+K # Kill to end of line
Ctrl+U # Kill to beginning of line
Ctrl+W # Kill previous word
Alt+D # Kill next word
Ctrl+Y # Yank (paste)
Ctrl+T # Transpose characters
Alt+T # Transpose words
set -o vi
Esc # Enter command mode
i # Insert mode
a # Append mode
A # Append at end of line
I # Insert at beginning of line
### Opciones y Configuraciones de Bashbash
!! # Previous command
!n # Command number n
!string # Last command starting with string
!?string # Last command containing string
oldnew # Replace old with new in previous command