2 Ways to Create Custom Terminal Commands (or Aliases)

Avoid repetitive commands by creating custom terminal commands

Batikan Sosun
Better Programming

--

Photo by Boitumelo Phetla on Unsplash

We regularly run some terminal commands when we work on software or look at something on the network. Sometimes these commands can be annoying.

I am going to explain how we can do that in 2 ways.

Create Custom Command File

The first way, you can create a custom shell script file, and then you can add custom commands as functions. Let’s do it.

Open your terminal, and then go root directory, active user. You can go to the directory by cd ~(option + N).

We will work in the root directory because the .bash_profile file is placed here. By the way, these files have a . at the beginning of their name you can list them with ls -a command.

Anyway, we will create a file that stores our custom commands.

touch ~/.custom_bash_commands.sh

With this command, your custom command file will be created. You can name another one.

Open the file by the below command. This will open the file with TextEdit default Mac text editor. You can also put another editor name like Sublime Text.

open .custom_bash_commands.sh -a “TextEdit”

Add the following code into the file, and then save. This will print Hello + your written parameter.

#!/bin/bashfunction hello() {
echo “hello” $1 “!”
}

And then we need to evaluate this without restarting the terminal by the following command. If you don’t want that you can restart your terminal.

source .custom_bash_commands.sh

Back to the terminal, and then run your command like below. By the way, you can run that anywhere you would like.

hello world

output: hello world !

Good jobs. You created your custom command.

Now, I am going to give a real instance for the custom command. If you work with Git(version control system) you have to run some annoying git command. Like git add . and git commit -m “message”, and git push origin master.

Now, we can merge these and reduce them.

function commit_changes() {
git add .
git commit -m “$1”
git push origin $2
}

Add the above function to your custom command file, and then save the file. Run the below code to evaluate .bash_profile

source .custom_bash_commands.sh

And then just run the commit_changes command like below. That will commit and push your changes to the master branch with commit_message.

commit_changes commit_message master

Good jobs. Now, back to the second way.

Create Custom Alias

The second way is creating a custom alias. With an alias prefix, you can customize commands. I am going to explain the alias in the same example as I mentioned above.

alias commit_changes =”git add . && git commit -m ‘$1’ && git push origin $2”

And then link that with .bash_profile by the below command.

source .custom_bash_commands.sh

Without creating a custom command file, you can also add that to the .bash_profile file. Don’t forget to run the below command.

source .bash_profile

Another simple example for alias, you can do like below

alias go_project_dir = “cd /Users/Desktop/project/”

These are up to you. You can customize commands as you wish.

Note that working on the shell scripting is fun. These are facilitating your work. Feel free to use them.

--

--

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging