Building Your Bash Toolkit: Getting Started

Sep 18, 2023 by Frederic Hallot | 396 views

CSC bash Tools Bash-Toolkit

https://cylab.be/blog/284/building-your-bash-toolkit-getting-started

Welcome to the first installment of our series on developing your own set of Bash tools. For those who frequently work in the terminal, you'll understand the immense value in having a suite of custom tools tailored to your needs. This series aims to guide you in building that toolkit, ensuring you streamline and simplify those recurring actions in the terminal.

Setting the Stage

Creating the Workspace

Our first step is to set up a dedicated directory for our Bash tools. The ~ signifies our home directory, and we're going to house our tools in ~/bash-tools.

if [ ! -d ~/bash-tools ]; then
    mkdir ~/bash-tools
    echo "~/bash-tools directory created."
else
    echo "~/bash-tools directory already exists."
fi

Laying the First Brick: Our First Tool

Let's kick things off by creating our first script file 01-basic-tools.sh, which will host our first function, hello-world.

echo 'hello-world() {
    echo "Hello, World!"
}' > ~/bash-tools/01-basic-tools.sh

# Append an alias for brevity
echo 'alias hw=hello-world' >> ~/bash-tools/01-basic-tools.sh

Auto-loading Our Tools

It's vital that our tools are available every time we open our terminal. Here's the command to append the code to your ~/.bashrc for sourcing all .sh files from the ~/bash-tools directory. Execute this directly to enable the autoloading of your tools.

echo 'for file in $(ls ~/bash-tools/*.sh | sort); do
    source $file
done' >> ~/.bashrc

Here we have to use a small workaround! When source(ing) from ~/.bashrc, ~ will The sort command ensures files are sourced in alphanumeric order, allowing us to dictate the exact order.

Testing Our Setup

After setting everything up, let's quickly verify that our tools are functioning correctly:

  1. Open a new terminal session or source your .bashrc with source ~/.bashrc.
  2. Enter the command hw (which is an alias for hello-world). You should see the output Hello, World!.

If you encounter any issues, review the previous steps to ensure everything was executed correctly.

Recap

In this blog post, we've achieved the following:

  • Established a dedicated directory, ~/bash-tools, as our workspace.
  • Introduced the foundation of our toolkit with the hello-world function.
  • Ensured our tools are accessible in every terminal session by updating the ~/.bashrc file.

The steps laid out today form the cornerstone of our Bash toolkit journey, paving the way for advanced techniques and tools that we will explore in subsequent posts.

Conclusion

Building a customized suite of Bash tools isn't just about automating tasks or reducing keystrokes—it's about crafting an environment where you feel in control, efficient, and capable. It's the manifestation of your understanding of the system, tailored to your workflow. This journey you've embarked on is about enhancing your technical agility, making the terminal an even more potent tool in your arsenal. While today we started with a simple "Hello, World!", imagine the possibilities as we progressively elevate our toolkit's capabilities. Here's to the start of a journey in mastering and customizing our command-line experience!

What's Next?

In our upcoming posts, we'll be diving deeper, exploring more advanced and dynamic Bash tools. Our next article, Creating a Dynamic Argument Processor in Bash, will guide you through constructing a powerful function to seamlessly handle both direct arguments and piped data. Stay tuned, and happy scripting!

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept