DEV Community 👩‍💻👨‍💻

Jacob Knaack
Jacob Knaack

Posted on

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.

Top comments (0)

Join us at DEV

Find what you were looking for? Join hundreds of thousands of developers on DEV so you can:

 
🌚 Enable dark mode
🔠 Change your default font
📚 Adjust your experience level to see more relevant content