DEV Community

Richard Lenkovits
Richard Lenkovits

Posted on

10 1 1 1 1

Simple bash loading animation

I was looking for a simple way to achieve a basic load icon that ticks after a piece of text in bash, but there was noting I could just easily copy paste. Now I'll provide one for you, no shenanigans, no messing around.

Short story

I've found this one, Silejonu's solution on Github but this is rather a module than something you can copy paste, and sometimes you don't want to add whole dependencies, just a piece of logic.

So here it is:



function loading_icon() {
local load_interval="${1}"
local loading_message="${2}"
local elapsed=0
local loading_animation=( '—' "\" '|' '/' )

<span class="nb">echo</span> <span class="nt">-n</span> <span class="s2">"</span><span class="k">${</span><span class="nv">loading_message</span><span class="k">}</span><span class="s2"> "</span>

<span class="c"># This part is to make the cursor not blink</span>
<span class="c"># on top of the animation while it lasts</span>
tput civis
<span class="nb">trap</span> <span class="s2">"tput cnorm"</span> EXIT
<span class="k">while</span> <span class="o">[</span> <span class="s2">"</span><span class="k">${</span><span class="nv">load_interval</span><span class="k">}</span><span class="s2">"</span> <span class="nt">-ne</span> <span class="s2">"</span><span class="k">${</span><span class="nv">elapsed</span><span class="k">}</span><span class="s2">"</span> <span class="o">]</span><span class="p">;</span> <span class="k">do
    for </span>frame <span class="k">in</span> <span class="s2">"</span><span class="k">${</span><span class="nv">loading_animation</span><span class="p">[@]</span><span class="k">}</span><span class="s2">"</span> <span class="p">;</span> <span class="k">do
        </span><span class="nb">printf</span> <span class="s2">"%s</span><span class="se">\b</span><span class="s2">"</span> <span class="s2">"</span><span class="k">${</span><span class="nv">frame</span><span class="k">}</span><span class="s2">"</span>
        <span class="nb">sleep </span>0.25
    <span class="k">done
    </span><span class="nv">elapsed</span><span class="o">=</span><span class="k">$((</span> elapsed <span class="o">+</span> <span class="m">1</span> <span class="k">))</span>
<span class="k">done
</span><span class="nb">printf</span> <span class="s2">" </span><span class="se">\b\n</span><span class="s2">"</span>
Enter fullscreen mode Exit fullscreen mode

}

loading_icon 60 "I'm loading!"

Enter fullscreen mode Exit fullscreen mode




And here's what it looks like:

bash loading animation

Limitations

  • Shows a given loading_message and after it a small loading icon for the provided load_interval seconds.
  • After the provided time elapsed the last character of the loading icon is erased and a new line is started (this can be changed by updating printf " \b\n").

You can get more animation ideas from Silejonu, bash_loading_animations file, all you have to do is to replace the loading_animation variable in the script (don't forget to remove the first element, the interval number).

Enjoy! ^^

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️