Last active
September 5, 2025 07:48
-
-
Save zzf01/5c84d9fdd60e321cd7741683e7888dd1 to your computer and use it in GitHub Desktop.
enhanced .bashrc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| BASHRC="${HOME}/.bashrc" | |
| BASH_ALIASES="${HOME}/.bash_aliases" | |
| PROFILE="${HOME}/.profile" | |
| LSC_DIR="${HOME}/.lscolors" | |
| LSC_SCRIPT="${LSC_DIR}/lscolors.sh" | |
| LSC_URL="https://raw.githubusercontent.com/ahmadassaf/dircolors/refs/heads/master/lscolors.sh" | |
| # 0) Require ~/.bashrc to exist | |
| if [ ! -f "$BASHRC" ]; then | |
| echo "Error: ${BASHRC} does not exist. Aborting." >&2 | |
| exit 1 | |
| fi | |
| # 1) Ensure alias l="ls -al" exactly once | |
| alias_present=false | |
| # Check current shell | |
| if alias l >/dev/null 2>&1; then | |
| alias_present=true | |
| fi | |
| # Check .bashrc, .bash_aliases, .profile | |
| for f in "$BASHRC" "$BASH_ALIASES" "$PROFILE"; do | |
| if [ -f "$f" ] && grep -Eq '^[[:space:]]*alias[[:space:]]+l=' "$f"; then | |
| alias_present=true | |
| fi | |
| done | |
| if $alias_present; then | |
| echo 'Alias l already present (in shell or config).' | |
| else | |
| echo 'alias l="ls -al"' >> "$BASHRC" | |
| echo "Added alias l to ${BASHRC}" | |
| fi | |
| # 2) Ensure ~/.lscolors exists and fetch lscolors.sh if directory missing | |
| if [ ! -d "$LSC_DIR" ]; then | |
| mkdir -p "$LSC_DIR" | |
| echo "Created ${LSC_DIR}" | |
| if command -v curl >/dev/null 2>&1; then | |
| curl -L -o "$LSC_SCRIPT" "$LSC_URL" | |
| elif command -v wget >/dev/null 2>&1; then | |
| wget -O "$LSC_SCRIPT" "$LSC_URL" | |
| else | |
| echo "Error: neither curl nor wget is installed. Please install one and re-run." >&2 | |
| exit 1 | |
| fi | |
| echo "Downloaded lscolors.sh to ${LSC_SCRIPT}" | |
| else | |
| echo "${LSC_DIR} already exists; skipping download step." | |
| fi | |
| # 3) Ensure sourcing line is present exactly once in ~/.bashrc | |
| SOURCE_LINE='source ~/.lscolors/lscolors.sh' | |
| if ! grep -Fxq "$SOURCE_LINE" "$BASHRC"; then | |
| echo "$SOURCE_LINE" >> "$BASHRC" | |
| echo "Added sourcing line to ${BASHRC}" | |
| else | |
| echo "Sourcing line already present in ${BASHRC}" | |
| fi | |
| # 4) Add optimized prompt block (no chroot info), portable across distros | |
| PROMPT_MARKER_BEGIN="# BEGIN zz custom prompt" | |
| PROMPT_MARKER_END="# END zz custom prompt" | |
| add_custom_prompt() { | |
| cat >> "$BASHRC" <<'BASHRC_EOF' | |
| # BEGIN zz custom prompt | |
| # Only set PS1 for interactive shells | |
| if [[ $- == *i* ]]; then | |
| # Show "(screen)" when inside GNU screen | |
| if [[ -n "$STY" ]]; then | |
| SCREEN_INDICATOR="(screen)" | |
| else | |
| SCREEN_INDICATOR="" | |
| fi | |
| # Keep hostname dynamic via hostname -f | |
| PROMPT_HOST=$(hostname -f) | |
| # Prompt: | |
| # - \u user, \w cwd, \$ root/# vs user/$ | |
| # - Colors wrapped in \[ \] to keep readline happy | |
| PS1="\[\e[1;31m\]\u\[\e[1;33m\]@\[\e[1;36m\]${PROMPT_HOST} \[\e[1;33m\]\w \[\e[1;35m\]${SCREEN_INDICATOR} \$ \[\e[0m\]" | |
| fi | |
| # END zz custom prompt | |
| BASHRC_EOF | |
| } | |
| # Detect PS1 lines (at line start, allowing leading spaces) | |
| ps1_lines=$(grep -nE '^[[:space:]]*PS1=' "$BASHRC" || true) | |
| if [ -z "$ps1_lines" ]; then | |
| # No PS1 found — add our custom prompt | |
| add_custom_prompt | |
| echo "Added custom prompt to ${BASHRC}" | |
| else | |
| # Check whether any existing PS1 already looks like our custom one | |
| if grep -Eq '^[[:space:]]*PS1=.*PROMPT_HOST.*SCREEN_INDICATOR' "$BASHRC"; then | |
| echo "Existing PS1 appears to be the custom prompt already; leaving it as-is." | |
| else | |
| echo "A PS1 line exists in ${BASHRC}, and it is not the custom one." | |
| reply="" | |
| if ! read -rp "Replace existing PS1 with the custom prompt? [y/N] " reply </dev/tty; then | |
| reply="n" | |
| fi | |
| case "${reply:-n}" in | |
| [yY][eE][sS]|[yY]) | |
| # Comment out all leading PS1= lines (backup created) | |
| sed -i.bak '/^[[:space:]]*PS1=/ s/^/# disabled by bashrc-enhancer: /' "$BASHRC" | |
| add_custom_prompt | |
| echo "Replaced existing PS1 with custom prompt. Backup saved as ${BASHRC}.bak" | |
| ;; | |
| *) | |
| echo "Keeping existing PS1. No custom prompt added." | |
| ;; | |
| esac | |
| fi | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment