---
title: "Code Snippets - Reusable Code Examples and Solutions"
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/codesnippets/page/2
---

# Code Snippets

A curated collection of useful code snippets, examples, and ready-to-use solutions for various programming languages, frameworks, and common development scenarios. Copy, paste, and adapt for your projects.

[All Snippets16](/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)

[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)[Python5](/codesnippets/python)[Typescript2](/codesnippets/typescript)

[![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)

[![Optimizing your python code with \_\_slots\_\_?](/_astro/hero.DP_vYsHU_gYDXn.webp)](/codesnippets/post/python-slots-optimization)

## [Optimizing your python code with \_\_slots\_\_?](/codesnippets/post/python-slots-optimization)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Python](/codesnippets/categories/python)
-   [Productivity](/codesnippets/categories/productivity)
-   Published: 16 Jul, 2025

Memory Optimization with slots Understanding the Problem Dev Tip: Optimizing Data Models in Big Data Workflows with slots In big data and MLOps workflows, you often work with

[#Python](/codesnippets/tags/python)[#MemoryOptimization](/codesnippets/tags/memoryoptimization)[#DataScience](/codesnippets/tags/datascience)+4 tags

[Python](/codesnippets/python)

[View Code](/codesnippets/post/python-slots-optimization)

[![List S3 Buckets](/_astro/hero.BsiJ6hry_1X9XLn.webp)](/codesnippets/post/python-list-s3-buckets)

## [List S3 Buckets](/codesnippets/post/python-list-s3-buckets)

-   [Mohammad Abu Mattar](/authors/mohammad-abu-mattar)
-   [Aws](/codesnippets/categories/aws)
-   [Python](/codesnippets/categories/python)
-   [Devops](/codesnippets/categories/devops)
-   Published: 25 Jun, 2025

Overview Multi-Profile S3 Management Multi-Profile S3 Safari! Ever juggled multiple AWS accounts and needed a quick S3 bucket inventory across all of them? This Python script is your guid

[#Python](/codesnippets/tags/python)[#Boto3](/codesnippets/tags/boto3)[#AWS](/codesnippets/tags/aws)+5 tags

[Python](/codesnippets/python)

[View Code](/codesnippets/post/python-list-s3-buckets)

[![AWS Secrets Manager](/_astro/hero.BIDl4oI2_Z1yJEXf.webp)](/codesnippets/post/nodejs-aws-secrets-manager)

## [AWS Secrets Manager](/codesnippets/post/nodejs-aws-secrets-manager)

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

Need to load secrets in your Node.js app without exposing them? Here's how. If you're still storing API keys or database credentials in .env files or hardcoding them into your codebase, it's ti

[#NodeJS](/codesnippets/tags/nodejs)[#TypeScript](/codesnippets/tags/typescript)[#AWS](/codesnippets/tags/aws)+3 tags

[Typescript](/codesnippets/typescript)

[View Code](/codesnippets/post/nodejs-aws-secrets-manager)

[![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)

[](/codesnippets)

[1](/codesnippets)[2](/codesnippets/page/2)

Page 2 of 2

##### [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)
-   [Python3](/codesnippets/categories/python)
-   [Async1](/codesnippets/categories/async)
-   [Networking1](/codesnippets/categories/networking)
-   [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)[#Python6](/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)[#Performance4](/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)[#Aiohttp1](/codesnippets/tags/aiohttp)[#Asyncio1](/codesnippets/tags/asyncio)[#Concurrency1](/codesnippets/tags/concurrency)[#Http1](/codesnippets/tags/http)[#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)
