Sep 18, 2023 by Frederic Hallot | 604 views
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.
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
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
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.
After setting everything up, let's quickly verify that our tools are functioning correctly:
.bashrc
with source ~/.bashrc
.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.
In this blog post, we've achieved the following:
~/bash-tools
, as our workspace.hello-world
function.~/.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.
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!
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