Getting Started with Bash Scripting update

I just thought I’d do a quick update on the last blog post I did on getting started with Bash scripting.

Here’s an even better starter script, that does all the boilerplate and setup for you, but has the added bonus of working for Python scripts as well. So you can create a Bash script with my-bash-script sh or create a Python script with my-python-script py and the script-starter will do the rest, adding the right boilerplate to each and giving you an “hello” to let you know the script is up and running.

The new “hello” part is an improvement on my last example where I didn’t have the “hello” properly setup to call from the new script.

Anyway this one works perfectly, I’ve tested it thoroughly:


# Usage
# $ filename extension

# give bash extensions more descriptive names

# create script in bin folder
touch ~/bin/"$filename"."$extension"

# setup bash or python boilerplate and add an echo "hello..."
if [ $extension == 'sh' ]
    echo '#!/bin/bash' >> ~/bin/"$filename".sh
    echo 'echo "hello from your new bash script"' >> ~/bin/"$filename".sh
elif [ $extension == 'py' ]
    echo '#!/usr/bin/env python3' >> ~/bin/"$filename".py
    echo 'print("hello from your new python script")' >> ~/bin/"$filename".py

# sort out permissions
chmod 755 ~/bin/"$filename"."$extension"
# call the script to test it works
# give me a chance to read the "hello"
sleep 1
# open script with nvim
nvim ~/bin/"$filename"."$extension"