.bash_profile und .bashrc sind Konfigurationsdateien für die Bash-Shell. Sie werden beim Start einer Shell automatisch ausgeführt und ermöglichen es, Umgebungsvariablen zu setzen, Aliase zu definieren und das Shell-Verhalten anzupassen.
| Datei | Wann wird sie geladen? | Verwendung |
|---|---|---|
| .bash_profile | Login-Shell (SSH, Console Login) | Umgebungsvariablen, PATH, einmalige Setup-Aufgaben |
| .bashrc | Interaktive Non-Login-Shell (neues Terminal) | Aliase, Funktionen, Prompt-Anpassung |
| .profile | Login-Shell (wenn .bash_profile fehlt) | POSIX-kompatible Alternative |
Beim Anmelden via SSH oder Konsole:
1. /etc/profile 2. ~/.bash_profile (wenn vorhanden) ODER ~/.bash_login (wenn vorhanden) ODER ~/.profile
Beim Öffnen eines neuen Terminals in der grafischen Oberfläche:
1. /etc/bash.bashrc 2. ~/.bashrc
# ~/.bash_profile # PATH erweitern export PATH="$HOME/bin:$HOME/.local/bin:$PATH" # Umgebungsvariablen setzen export EDITOR=vim export VISUAL=vim export BROWSER=firefox # .bashrc laden (wichtig!) if [ -f ~/.bashrc ]; then . ~/.bashrc fi # Einmalige Aufgaben beim Login echo "Willkommen, $(whoami)!"
# ~/.bashrc # Wenn nicht interaktiv, abbrechen [[ $- != *i* ]] && return # History-Einstellungen HISTSIZE=10000 HISTFILESIZE=20000 HISTCONTROL=ignoredups:ignorespace shopt -s histappend # Prompt anpassen PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' # Aliase alias ll='ls -lah' alias la='ls -A' alias l='ls -CF' alias grep='grep --color=auto' alias ..='cd ..' alias ...='cd ../..' # Funktionen mkcd() { mkdir -p "$1" && cd "$1" } # Auto-Completion aktivieren if [ -f /etc/bash_completion ]; then . /etc/bash_completion fi
# In ~/.bash_profile export PATH="$HOME/bin:$PATH" export PATH="$HOME/.local/bin:$PATH" export PATH="/opt/myapp/bin:$PATH" # Eigene Programme verfügbar machen mkdir -p ~/bin echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bash_profile
# In ~/.bash_profile export EDITOR=vim export VISUAL=vim export PAGER=less export BROWSER=firefox export LANG=de_DE.UTF-8 export LC_ALL=de_DE.UTF-8 # Anwendungsspezifisch export JAVA_HOME=/usr/lib/jvm/java-17-openjdk export GOPATH=$HOME/go export NODE_ENV=development
# In ~/.bashrc # Sicherheitsabfragen alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # Verzeichnis-Navigation alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..' alias ~='cd ~' alias -- -='cd -' # ls-Varianten alias ll='ls -lah' alias la='ls -A' alias l='ls -CF' alias lt='ls -ltr' # Nach Zeit sortiert # Git-Aliase alias gs='git status' alias ga='git add' alias gc='git commit' alias gp='git push' alias gl='git log --oneline' # System alias update='sudo apt update && sudo apt upgrade' alias ports='netstat -tulanp' alias myip='curl ifconfig.me' # Farbige Ausgabe alias grep='grep --color=auto' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias diff='diff --color=auto'
# In ~/.bashrc # Verzeichnis erstellen und hineinwechseln mkcd() { mkdir -p "$1" && cd "$1" } # Datei erstellen und in Editor öffnen touchedit() { touch "$1" && $EDITOR "$1" } # Archiv extrahieren (automatische Format-Erkennung) extract() { if [ -f "$1" ]; then case "$1" in *.tar.bz2) tar xjf "$1" ;; *.tar.gz) tar xzf "$1" ;; *.bz2) bunzip2 "$1" ;; *.gz) gunzip "$1" ;; *.tar) tar xf "$1" ;; *.zip) unzip "$1" ;; *.Z) uncompress "$1" ;; *.7z) 7z x "$1" ;; *) echo "Unbekanntes Format: '$1'" ;; esac else echo "Datei '$1' nicht gefunden" fi } # Größe eines Verzeichnisses dirsize() { du -sh "${1:-.}" } # Backup einer Datei erstellen backup() { cp "$1" "$1.backup-$(date +%Y%m%d-%H%M%S)" }
# Einfacher Prompt: user@host:dir$ PS1='\u@\h:\w\$ ' # Mit Farben: grüner User, blauer Pfad PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' # Mit Uhrzeit PS1='[\t] \u@\h:\w\$ ' # Root-User rot hervorheben if [ "$EUID" -eq 0 ]; then PS1='\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' fi
# Git-Branch anzeigen parse_git_branch() { git branch 2>/dev/null | grep '^*' | colrm 1 2 } PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;33m\]$(parse_git_branch)\[\033[00m\]\$ '
# ~/.bash_profile export PATH="$HOME/bin:$HOME/.local/bin:$PATH" export EDITOR=vim export VISUAL=vim # Node.js export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # Python export PYTHONPATH="$HOME/python-libs:$PYTHONPATH" alias python=python3 alias pip=pip3 # Go export GOPATH=$HOME/go export PATH=$PATH:$GOPATH/bin # Rust export PATH="$HOME/.cargo/bin:$PATH"
# ~/.bashrc # Sicherheitsaliase alias rm='rm -i' alias mv='mv -i' alias cp='cp -i' # System-Monitoring alias cpu='top -o %CPU' alias mem='top -o %MEM' alias ports='netstat -tulanp' # Logs alias syslog='sudo tail -f /var/log/syslog' alias auth='sudo tail -f /var/log/auth.log' # Services alias services='systemctl list-units --type=service' alias failed='systemctl --failed' # Disk-Space alias disk='df -h | grep -v loop' alias biggest='du -h / | sort -rh | head -20'
# In ~/.bashrc # History-Größe HISTSIZE=10000 HISTFILESIZE=20000 # Duplikate ignorieren HISTCONTROL=ignoredups:ignorespace # Zeitstempel in History HISTTIMEFORMAT="%F %T " # History sofort speichern shopt -s histappend PROMPT_COMMAND="history -a" # Bestimmte Befehle nicht speichern HISTIGNORE="ls:ll:cd:pwd:exit:clear:history"
# .bashrc bearbeiten nano ~/.bashrc # Oder mit vim vim ~/.bashrc # Änderungen neu laden (ohne Logout) source ~/.bashrc # Oder . ~/.bashrc # .bash_profile bearbeiten nano ~/.bash_profile source ~/.bash_profile
Systemweit für alle Benutzer bei Login-Shells:
# /etc/profile export PATH="/usr/local/bin:/usr/bin:/bin" export EDITOR=vim # Benutzerspezifische Profile laden for i in /etc/profile.d/*.sh; do if [ -r "$i" ]; then . "$i" fi done
Systemweit für alle Benutzer bei interaktiven Shells:
# /etc/bash.bashrc alias ll='ls -l' PS1='\u@\h:\w\$ '
Vorlage für neue Benutzer:
# Standard-Dateien für neue Benutzer ls -la /etc/skel/ # .bashrc # .bash_profile # .profile
cp ~/.bashrc ~/.bashrc.backupsource ~/.bashrc testen vor Logout# Shell im Debug-Modus starten bash -x # Oder in .bashrc: set -x # Debug an # ... Code ... set +x # Debug aus # Prüfen welche Datei geladen wird echo "Loading .bashrc" # Startup-Zeit messen time bash -c exit # Login-Shell testen bash --login # Non-Login-Shell testen bash
# Neu laden source ~/.bashrc # Oder neue Shell starten bash # Prüfen ob richtige Datei bearbeitet wurde echo $HOME cat ~/.bashrc | grep "meine-änderung"
# In .bash_profile statt .bashrc setzen # Und sicherstellen dass .bash_profile .bashrc lädt # Prüfen: echo $PATH
# Aliase sind nur für interaktive Shells # In Skripten Funktionen oder volle Befehle nutzen # Oder Alias expandieren: shopt -s expand_aliases