Search code examples
macosterminalcommandzsh

how to create custom zsh commands?


I want to make my own zsh commands on my mac. And I have no idea how to do that and how to write shell code. How do I get started? I want to make a basic command like this:

myname@My-MacBook-Air  ~ hi
hi, user!

Solution

  • You can write a bash script and save it in your /usr/bin/ directory or any other directory. for example:

    Step 1

    Create a new file and name it mycommand.sh. then write the below code in it.

    #!/bin/bash
    echo "Hello"
    

    Step 2

    Set an executable access to this file by running this command in the terminal.

    > chmod +x mycommand.sh
    

    Step 3

    Make sure the /usr/bin/ directory is in your PATH by running this command in the terminal.

    > echo $PATH
    

    If there is not, visit this website to learn how to add a directory to the PATH.

    Step 4

    Run the command mycommand in your terminal and you will see the "Hello" string in the terminal.

    > mycommand
    Hello
    

    You can write more complex scripts in that file and create your own commands.