Define and Execute custom shell function on Unix Bash or Mac Zsh

Add the below function to ~/.bash_profile or ~/.zprofile file


# Continuously print the current timestamp until Ctrl+C is pressed
clockon() {
    # Clear the screen initially (optional)
    clear
    echo "Press [CTRL+C] to stop."
    echo "------------------------"
    
    while true; do
        # \r overwrites the current line instead of scrolling down
        printf "\rCurrent Time: %s" "$(date '+%Y-%m-%d %H:%M:%S')"
        sleep 1
    done
}

Source the file or close and reopen the Terminal


    source ~/.bash_profile 

or 

    source ~/.zprofile

List the function available on the shell


    compgen -A function | grep 'clockon'

Execute the function


    clockon 

Comments

Popular posts from this blog

Export/Backup Ollama Model

Download and run Hugging Face AI models in Ollama

Import self signed in Linux for Chrome / Chromium headless testing