# 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
Bash (Bourne Again Shell) è una shell Unix e un linguaggio di comandi scritto da Brian Fox per il Progetto GNU come sostituto gratuito della Bourne Shell. Rilasciato per la prima volta nel 1989, Bash è diventato la shell predefinita sulla maggior parte delle distribuzioni Linux ed è una delle shell più utilizzate nell'ecosistema Unix/Linux. Combina le funzionalità della shell Bourne originale con funzionalità aggiuntive tra cui il completamento dei comandi, la cronologia dei comandi e capacità di scripting migliorate.
Note:
- I've maintained the markdown formatting
- Technical terms like "Bash" remain in English
- The structure and punctuation are preserved
- Translations are provided in the same numbered format
Would you like me to proceed with translating the specific sections you mentioned?```bash
# Variable assignment (no spaces around =)name="John Doe"age=30path="/home/user"# Using variablesecho$nameecho$\\\\{name\\\\}echo"Hello, $name"# Environment variablesexportPATH="/usr/local/bin:$PATH"exportEDITOR="vim"# Special variablesecho$0# Script nameecho$1# First argumentecho$## Number of argumentsecho$@# All argumentsecho$# Process IDecho$?# Exit status of last command
# Using $() (preferred)current_date=$(date)file_count=$(ls|wc-l)user_home=$(evalecho~$USER)# Using backticks (legacy)current_date=`date`file_count=`ls|wc-l`# Nested command substitutionecho"Today is $(date+%A), $(date+%B)$(date+%d)"
# Single quotes (literal)echo'The variable $HOME is not expanded'# Double quotes (variable expansion)echo"Your home directory is $HOME"# Escaping special charactersecho"The price is \$10"echo"Use \"quotes\" inside quotes"# Here documentscat<< EOFThis is a multi-linetext block that can containvariables like $HOMEEOF# Here stringsgrep"pattern"<<<"$variable"
# Change directorycd/path/to/directory
cd~# Home directorycd-# Previous directorycd..# Parent directorycd../..# Two levels up# Print working directorypwd# Directory stack operationspushd/path/to/dir# Push directory onto stackpopd# Pop directory from stackdirs# Show directory stack
# Basic listingls
ls-l# Long formatls-la# Long format with hidden filesls-lh# Human readable sizesls-lt# Sort by modification timels-lS# Sort by sizels-lR# Recursive listing# Advanced listing optionsls-la--color=auto# Colored outputls-la--time-style=full-iso# ISO time formatls-la--group-directories-first# Directories first
# Create filestouchfile.txt
touchfile1.txtfile2.txtfile3.txt
# Copy files and directoriescpsource.txtdestination.txt
cp-rsource_dir/destination_dir/
cp-pfile.txtbackup.txt# Preserve attributescp-usource.txtdest.txt# Update only if newer# Move and renamemvold_name.txtnew_name.txt
mvfile.txt/path/to/destination/
mv*.txt/path/to/directory/
# Remove files and directoriesrmfile.txt
rm-ffile.txt# Force removalrm-rdirectory/# Recursive removalrm-rfdirectory/# Force recursive removalrm-i*.txt# Interactive removal# Create directoriesmkdirdirectory_name
mkdir-ppath/to/nested/directory
mkdir-m755directory_name# With specific permissions
# grep - pattern searchinggrep"pattern"file.txt
grep-i"pattern"file.txt# Case insensitivegrep-r"pattern"directory/# Recursive searchgrep-n"pattern"file.txt# Show line numbersgrep-v"pattern"file.txt# Invert matchgrep-E"pattern1|pattern2"file.txt# Extended regex# Advanced grep optionsgrep-A3"pattern"file.txt# Show 3 lines after matchgrep-B3"pattern"file.txt# Show 3 lines before matchgrep-C3"pattern"file.txt# Show 3 lines around matchgrep-l"pattern"*.txt# Show only filenamesgrep-c"pattern"file.txt# Count matches
# sed - stream editorsed's/old/new/'file.txt# Replace first occurrencesed's/old/new/g'file.txt# Replace all occurrencessed'1,5s/old/new/g'file.txt# Replace in lines 1-5sed'/pattern/d'file.txt# Delete lines matching patternsed-n'1,10p'file.txt# Print lines 1-10# awk - pattern scanning and processingawk'\\\\{print $1\\\\}'file.txt# Print first columnawk'\\\\{print $NF\\\\}'file.txt# Print last columnawk'/pattern/ \\\\{print $0\\\\}'file.txt# Print lines matching patternawk-F:'\\\\{print $1\\\\}'/etc/passwd# Use : as field separatorawk'\\\\{sum += $1\\\\} END \\\\{print sum\\\\}'file.txt# Sum first column# cut - extract columnscut-d:-f1/etc/passwd# Extract first fieldcut-c1-10file.txt# Extract characters 1-10cut-f2,4file.txt# Extract fields 2 and 4# sort and uniqsortfile.txt# Sort linessort-nfile.txt# Numeric sortsort-rfile.txt# Reverse sortsort-k2file.txt# Sort by second fielduniqfile.txt# Remove duplicate linessortfile.txt|uniq-c# Count occurrences
# Output redirectioncommand>file.txt# Redirect stdout to file (overwrite)command>>file.txt# Redirect stdout to file (append)command2>error.log# Redirect stderr to filecommand2>>error.log# Redirect stderr to file (append)command&>output.log# Redirect both stdout and stderrcommand>output.log2>&1# Redirect both stdout and stderr# Input redirectioncommand<input.txt# Read input from filecommand<< EOF # Here documentline 1line 2EOF# Advanced redirectioncommand3>file.txt# Redirect to file descriptor 3exec3>file.txt# Open file descriptor 3echo"text">&3# Write to file descriptor 3exec3>&-# Close file descriptor 3
# Basic pipesls-l|grep"txt"# List files and filter for .txtpsaux|grep"process_name"# Show processes and filtercatfile.txt|sort|uniq# Sort and remove duplicates# Complex pipe chainscat/var/log/access.log|grep"404"|awk'\\\\{print $1\\\\}'|sort|uniq-c|sort-nr
find.-name"*.log"|xargsgrep"ERROR"|cut-d:-f1|sort|uniq
# Tee command (split output)command|teefile.txt# Write to file and stdoutcommand|tee-afile.txt# Append to file and stdout
# Background and foreground jobscommand&# Run command in backgroundjobs# List active jobsfg%1# Bring job 1 to foregroundbg%1# Send job 1 to backgroundkill%1# Kill job 1# Process control signalsCtrl+C# Interrupt (SIGINT)Ctrl+Z# Suspend (SIGTSTP)Ctrl+\ # Quit (SIGQUIT)
# Process listingps# Show current processespsaux# Show all processesps-ef# Full format listingpstree# Show process treetop# Real-time process monitorhtop# Enhanced process monitor# Process managementkillPID# Terminate processkill-9PID# Force kill processkillallprocess_name# Kill all processes by namepkillpattern# Kill processes matching patternnohupcommand&# Run command immune to hangups
echo $\\{fruits[0]\\} # First element
echo $\\{fruits[@]\\} # All elements
echo $\\{#fruits[@]\\} # Array length
fruits+=("grape") # Append element
### Istruzioni Condizionalibash
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
### Espansione dei Parametribash
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
### Opzioni e Impostazioni di 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