DEV Community

Jacob Knaack
Jacob Knaack

Posted on

1 1

Creating a Bash Command

Let's walk through the process of adding a fresh baby bash script as a command that you can run in your terminal.

Say you have a super rad script that does mega cool things somewhere on your computer:

hello-world

#!/bin/bash


echo Hello world

Allowing your bash terminal to run this script file as a command just takes a few simple steps.

1. Place your script somewhere that scripts go.

I recommend creating a directory at ~/ called bin or .bin. The bin folder is typically where commands go, so I'm going to replicate this:

$ cd ~/

$ mkdir .bin/

$ mv path/to/script ~/.bin/YOUR-SCRIPT-NAME

2. Add this Directory to your PATH.

Add a line to your .bash_profile that will make sure your bash terminal has a path to this file when running your command.

If you don't have one create one with

$ touch ~/.bash_profile

.bash_profile

#Here's the line you should add:
export PATH=$PATH:/Users/YOUR-USERNAME/.bin

Now you can either restart your terminal at this point or source this file:

source ~/.bash_profile

3. Add Permissions for Users to run this command.

We'll use the illusive chmod command to let users run this newly created command:

$ chmod u+x ~/.bin/YOUR-SCRIPT-NAME

And that's it! You should now be able to run your script from anywhere in your terminal.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more