Cancel

Write Your Own Command in Bash

Maybe you are thinking that how you can create your own bash command same as ls, echo, touch, rm, and etc.

Create a bin folder in the user’s home directory.

1
mkdir ~/bin

Sync hello.sh file with your future command called hello, and also whenever you update hello.sh file, it will automatically sync all updates to hello command.

1
ln -sf $PWD/hello.sh ~/bin/hello

To run your hello command, You need to set ~/bin path into your ~/.profile file. Copy and paste the following code into your ~/.profile, and don’t forget to restart your terminal.

1
2
3
if [[ -d "$HOME/bin" ]]; then
  PATH="$HOME/bin:$PATH"
fi

Now you can run your hello command like this.

1
2
3
4
5
6
7
8
9
10
 # option 1
/bin/hello
/bin/hello -v
/bin/hello -h
/bin/hello -f Yafiz -l Abraham

# option 2 (recommended)
hello -v
hello -h
hello -f Yafiz -l Abraham