---
title: "Bash Code Snippets"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/codesnippets/bash
---

6 Snippets ·Bash

# Bash Code Snippets

Browse code snippets written in Bash

[All Snippets15](/codesnippets)[Cloud1](/codesnippets/categories/cloud)[Aws4](/codesnippets/categories/aws)[Devops8](/codesnippets/categories/devops)[Automation2](/codesnippets/categories/automation)[Shell scripting6](/codesnippets/categories/shell-scripting)[Productivity3](/codesnippets/categories/productivity)[More…](/codesnippets/categories)

[All Snippets15](/codesnippets)[Bash and Python1](/codesnippets/bash-and-python)[Python, Node.js and Go1](/codesnippets/python-nodejs-and-go)[SQL and Python1](/codesnippets/sql-and-python)[Bash6](/codesnippets/bash)[Python4](/codesnippets/python)[Typescript2](/codesnippets/typescript)

[![Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/_astro/hero.Dap62rVN_Z1q0nar.webp)](/codesnippets/post/bash-retry-function-exponential-backoff)

## [Bash Retry Function: Automatically Retry Failing Commands with Exponential Backoff](/codesnippets/post/bash-retry-function-exponential-backoff)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Devops](/codesnippets/categories/devops)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Automation](/codesnippets/categories/automation)
-   Published: 20 Jul, 2026

Quick Tip Wrap any flaky command in one reusable retry function and stop re-running red pipelines by hand. The Problem The Problem Some commands fail for reasons that have nothing to

[#Bash](/codesnippets/tags/bash)[#Retry](/codesnippets/tags/retry)[#Exponential Backoff](/codesnippets/tags/exponential-backoff)+3 tags

[Bash](/codesnippets/bash)

[View Code](/codesnippets/post/bash-retry-function-exponential-backoff)

[![Why printf Beats echo in Linux Scripts](/_astro/hero.Dl3YkIwZ_Z1w9hux.webp)](/codesnippets/post/printf-beats-echo-linux-scripts)

## [Why printf Beats echo in Linux Scripts](/codesnippets/post/printf-beats-echo-linux-scripts)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)
-   [Linux](/codesnippets/categories/linux)
-   Published: 02 Jan, 2026

Scripting Tip You know that feeling when a script works perfectly on your machine but fails miserably somewhere else? That's probably because you're using echo for output. Let me show you why pri

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Printf](/codesnippets/tags/printf)+5 tags

[Bash](/codesnippets/bash)

[View Code](/codesnippets/post/printf-beats-echo-linux-scripts)

[![Essential Bash Variables for Every Script](/_astro/hero.B4bDowyY_YQpD7.webp)](/codesnippets/post/essential-bash-variables)

## [Essential Bash Variables for Every Script](/codesnippets/post/essential-bash-variables)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)
-   Published: 26 Dec, 2025

Overview Quick Tip You know what's worse than writing scripts? Writing scripts that break every time you move them to a different machine. Let's fix that with some built-in Bash variables tha

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Linux](/codesnippets/tags/linux)+4 tags

[Bash](/codesnippets/bash)

[View Code](/codesnippets/post/essential-bash-variables)

[![Per-App Shell History for Bash](/_astro/hero.Da_6jPH6_Z1EorRD.webp)](/codesnippets/post/bash-per-app-history)

## [Per-App Shell History for Bash](/codesnippets/post/bash-per-app-history)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Productivity](/codesnippets/categories/productivity)
-   Published: 18 Dec, 2025

Terminal Chaos? Organize Your Bash History! Ever jumped between iTerm2, Ghostty, and VS Code's terminal only to have your command history get all mixed up? This Bash snippet keeps things clean by

[#Bash](/codesnippets/tags/bash)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Productivity](/codesnippets/tags/productivity)+3 tags

[Bash](/codesnippets/bash)

[View Code](/codesnippets/post/bash-per-app-history)

[![Per-App Shell History for Zsh](/_astro/hero.DRenzVy__1xwzSL.webp)](/codesnippets/post/zsh-per-app-history)

## [Per-App Shell History for Zsh](/codesnippets/post/zsh-per-app-history)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Productivity](/codesnippets/categories/productivity)
-   Published: 18 Dec, 2025

Terminal Chaos? Organize Your Shell History! Ever jumped between iTerm2, Ghostty, and VS Code's terminal only to have your command history get all mixed up? This Zsh snippet keeps things clean by

[#Zsh](/codesnippets/tags/zsh)[#Shell Scripting](/codesnippets/tags/shell-scripting)[#Productivity](/codesnippets/tags/productivity)+3 tags

[Bash](/codesnippets/bash)

[View Code](/codesnippets/post/zsh-per-app-history)

[![Check S3 Bucket Existence](/_astro/hero.CWiO4GWV_1YUYTb.webp)](/codesnippets/post/bash-s3-bucket-exists)

## [Check S3 Bucket Existence](/codesnippets/post/bash-s3-bucket-exists)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Aws](/codesnippets/categories/aws)
-   [Shell scripting](/codesnippets/categories/shell-scripting)
-   [Devops](/codesnippets/categories/devops)
-   Published: 11 Jun, 2025

Quick Tip Don’t let your deployment blow up because of a missing S3 bucket. This Bash script lets you check if a bucket exists before anything fails clean and simple. The Problem Missi

[#Bash](/codesnippets/tags/bash)[#AWS](/codesnippets/tags/aws)[#DevOps](/codesnippets/tags/devops)+3 tags

[Bash](/codesnippets/bash)

[View Code](/codesnippets/post/bash-s3-bucket-exists)

##### [Categories](/codesnippets/categories)

-   [Cloud1](/codesnippets/categories/cloud)
-   [Aws4](/codesnippets/categories/aws)
-   [Devops8](/codesnippets/categories/devops)
-   [Automation2](/codesnippets/categories/automation)
-   [Shell scripting6](/codesnippets/categories/shell-scripting)
-   [Productivity3](/codesnippets/categories/productivity)
-   [Security1](/codesnippets/categories/security)
-   [Nodejs1](/codesnippets/categories/nodejs)
-   [Typescript1](/codesnippets/categories/typescript)
-   [Backend2](/codesnippets/categories/backend)
-   [Database1](/codesnippets/categories/database)
-   [Performance2](/codesnippets/categories/performance)
-   [Linux1](/codesnippets/categories/linux)
-   [Python2](/codesnippets/categories/python)
-   [Ai ml1](/codesnippets/categories/ai-ml)
-   [Computer vision1](/codesnippets/categories/computer-vision)
-   [Document processing1](/codesnippets/categories/document-processing)

##### [Tags](/codesnippets/tags)

[#Aws4](/codesnippets/tags/aws)[#Ec21](/codesnippets/tags/ec2)[#Boto32](/codesnippets/tags/boto3)[#Python5](/codesnippets/tags/python)[#Devops7](/codesnippets/tags/devops)[#Automation4](/codesnippets/tags/automation)[#Iam2](/codesnippets/tags/iam)[#Cloud2](/codesnippets/tags/cloud)[#Infrastructure1](/codesnippets/tags/infrastructure)[#Bash5](/codesnippets/tags/bash)[#Shell scripting5](/codesnippets/tags/shell-scripting)[#Productivity2](/codesnippets/tags/productivity)[#Devtools2](/codesnippets/tags/devtools)[#Terminal2](/codesnippets/tags/terminal)[#Configuration3](/codesnippets/tags/configuration)[#Retry1](/codesnippets/tags/retry)[#Exponential backoff1](/codesnippets/tags/exponential-backoff)[#Cicd2](/codesnippets/tags/cicd)[#Shell1](/codesnippets/tags/shell)[#S32](/codesnippets/tags/s3)[#Linux2](/codesnippets/tags/linux)[#Environment variables2](/codesnippets/tags/environment-variables)[#Posix2](/codesnippets/tags/posix)[#Vault1](/codesnippets/tags/vault)[#Secretsmanagement1](/codesnippets/tags/secretsmanagement)[#Multienvironment1](/codesnippets/tags/multienvironment)[#Security1](/codesnippets/tags/security)[#Nodejs3](/codesnippets/tags/nodejs)[#Typescript2](/codesnippets/tags/typescript)[#Secretsmanager1](/codesnippets/tags/secretsmanager)[#Cloudsecurity1](/codesnippets/tags/cloudsecurity)[#Zod1](/codesnippets/tags/zod)[#Validation1](/codesnippets/tags/validation)[#Dotenv1](/codesnippets/tags/dotenv)[#Backend2](/codesnippets/tags/backend)[#Postgresql1](/codesnippets/tags/postgresql)[#Queryoptimization1](/codesnippets/tags/queryoptimization)[#Indexes1](/codesnippets/tags/indexes)[#Performance3](/codesnippets/tags/performance)[#Sql1](/codesnippets/tags/sql)[#Database1](/codesnippets/tags/database)[#Microservices2](/codesnippets/tags/microservices)[#Printf1](/codesnippets/tags/printf)[#Echo1](/codesnippets/tags/echo)[#Scripting best practices1](/codesnippets/tags/scripting-best-practices)[#Scripting1](/codesnippets/tags/scripting)[#Memoryoptimization1](/codesnippets/tags/memoryoptimization)[#Datascience1](/codesnippets/tags/datascience)[#Mlops1](/codesnippets/tags/mlops)[#Bigdata1](/codesnippets/tags/bigdata)[#Bestpractices1](/codesnippets/tags/bestpractices)[#Redis1](/codesnippets/tags/redis)[#Caching1](/codesnippets/tags/caching)[#Go1](/codesnippets/tags/go)[#Optimization1](/codesnippets/tags/optimization)[#Ocr1](/codesnippets/tags/ocr)[#Computer vision1](/codesnippets/tags/computer-vision)[#Document processing1](/codesnippets/tags/document-processing)[#Open source1](/codesnippets/tags/open-source)[#Ai models1](/codesnippets/tags/ai-models)[#Machine learning1](/codesnippets/tags/machine-learning)[#Text recognition1](/codesnippets/tags/text-recognition)[#Zsh1](/codesnippets/tags/zsh)
