bash/

PS1 with git status without any extra dependencies

In this guide, we will add git status to the command line prompt without any extra dependencies.

Our new prompt has the following additional functionality:

  • If the command exits with a non-zero status, it will show the exit code in red, e.g., ✘127.
  • The username and hostname are shown in green, e.g., user@hostname (should prevent the user from executing commands on the wrong host).
  • The current working directory is shown in blue.
  • The git status is shown in square brackets (only if inside a git repository):
    • The branch name is shown in yellow.
    • Untracked files are shown with a yellow question mark.
    • Staged files are shown with a green plus sign.
    • Unstaged files are shown with a red exclamation mark.
    • Ahead and behind of upstream are shown with up and down arrows, respectively.
  • The prompt is split into multiple lines for better readability.

Open your ~/.bashrc file and add the following snippet:

function git_status_prompt() {
    local git_info=""
    if git rev-parse --is-inside-work-tree &>/dev/null; then
        # Define colors
        local branch_color="\033[0;33m"     # Yellow for branch name
        local reset_color="\033[0m"         # Reset to default color
        local untracked_color="\033[1;33m"  # Bold yellow for untracked
        local staged_color="\033[1;32m"     # Bold green for staged
        local unstaged_color="\033[1;31m"   # Bold red for unstaged

        # Show branch name if on a branch, otherwise show commit hash
        local branch_name=$(git symbolic-ref --short -q HEAD 2>/dev/null)
        if [[ -n "$branch_name" ]]; then
            git_info+="[${branch_color}${branch_name}${reset_color}"
        else
            git_info+="[${branch_color}@$(git rev-parse --short HEAD)${reset_color}"
        fi

        # Add additional status indicators with respective colors
        [[ -n $(git ls-files --others --exclude-standard) ]] && git_info+=" ${untracked_color}?${reset_color}"
        ! git diff --cached --quiet && git_info+=" ${staged_color}+${reset_color}"
        ! git diff --quiet && git_info+=" ${unstaged_color}!${reset_color}"

        # Ahead/behind of upstream
        local ahead=$(git rev-list --count @{u}..HEAD 2>/dev/null)
        local behind=$(git rev-list --count HEAD..@{u} 2>/dev/null)
        [[ $ahead -gt 0 ]] && git_info+=" ⇡$ahead"
        [[ $behind -gt 0 ]] && git_info+=" ⇣$behind"

        # Close the square bracket
        git_info+="]"
    fi
    echo -e "$git_info"
}
PS1='$(if [ $? -ne 0 ]; then echo "\[\e[0;31m\]✘$? \[\e[0m\] "; fi)\[\e[01;32m\]\u@\h\[\e[0m\] \[\e[01;34m\]\w\[\e[0m\] $(git_status_prompt)\n\$ '

Apply the changes by running:

source ~/.bashrc
| 3 Nov 2024

rygel: Disable inactivity timeout when streaming video

This script demonstrates how to disable system inactivity timeout in Gnome when running Rygel and restore it to the original value after exiting:

#!/bin/bash

# Save current inactivity timeout
TIMEOUT=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout)
echo "Current timeout: $TIMEOUT"

# Execute commands on Ctrl + C
function ctrl_c() {
  echo "Exiting rygel..."
  gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout $TIMEOUT
  echo "Inactivity timeout restored to $TIMEOUT. Bye!"
  sleep 2
}

# Set the trap for Ctrl+C
trap ctrl_c INT

# Disable the inactivity timeout
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 0

# Briefly start rygel to allow it to add new files
timeout 2s rygel

# Run command to fix filenames
rygel-titlefix

# Actually run rygel
rygel

| 17 Jul 2023

Wait until DNS propagates

until [[ -n $(dig new.mydomain.com A +short @1.1.1.1) ]]; do echo "waiting..." && sleep 5; done

-n flag stands for non-empty string

| 22 Dec 2021

Clear file from leading new line and etc

cat file | tr -d [$'\t\r\n'] > new_file
| 26 Feb 2021

Add exit code to the command line

Some examples to add exit code to command line prompt in bash (put it into ~/.bashrc):

export PS1="\[\033[01;31m\]\$([ \$? == 0 ] || echo \"!\$? \" )\[\033[00m\]\[\033[01;32m\]\t \[\033[01;34m\]\w\[\033[00m\]\[\033[1;32m\]\n\$ \[\033[m\]"

Add to the default PS1 in CentOS:

export PS1="\[\033[01;31m\]\${?##0}\[\033[00m\][\u@\h \W]\\$"
| 11 Jan 2021

Log executed command

14:27 $ (set -x; sleep 1 && sleep 2)
+ sleep 1
+ sleep 2
| 1 Jan 2020

Wait until able to connect

while ! nc -w5 -z 10.0.0.100 22; do echo "hello"; done
| 1 Jan 2020

How to check what hides behind an alias

To check what command is assigned to the alias use type command:

$ type gl
gl is aliased to `git log --oneline`
| 1 Jan 2020

Run command as a different user

Command runuser allows to run a command as a different user. You must be root to be able to run that command:

sudo runuser -l vagrant -c "GH=1 env"
| 1 Oct 2017

Get size of the subfolders

Use this command to print size of the subfolders:

du -sh *

If you want to exclude Permission denied errors, for example:

du -sh * 2>/dev/null
| 1 May 2017