Add programs to the PATH

Add programs to the PATH

Run a program from any path via CLI (command line).

in
Table of contents

The PATH environment variable is where the OS (operative system) search for needed executables when a command is executed from the CLI.

For example, if you want to run Kotlin scripts you need to add Kotlin to the PATH. There are different ways to add it based on the OS you are using.

In this example we are going to add Kotlin to the PATH, but you can add any program in a similar way.

You can download Kotlin from the official GitHub repository by looking at the last release and downloading the kotlin-compiler-$version.zip file, where $version is the version you are looking for, usually the latest one.

Linux

  1. Open the .bashrc file in your home directory with a text editor.
  2. Add the next line:

    export PATH="path/to/kotlin/bin/":$PATH
    

    If the executable is inside your home, you can use $HOME:

    export PATH="$HOME/path/to/kotlin/bin":$PATH
    
  3. Save the file.
  4. Restart the terminal.

macOS

  1. Open the .zshrc file in your home directory with a text editor.
  2. Add the next line:

    export PATH="path/to/kotlin/bin/":$PATH
    

    If the path is inside your home, you can use $HOME:

    export PATH="$HOME/path/to/kotlin/bin":$PATH
    
  3. Save the file.
  4. Restart the terminal.

Windows

  1. Press the Windows keyboard button and search for Environment variables and open Edit the system environment variables.
  2. In the new window, press the button Environment variables at the bottom.
  3. There are environment variables for your user or the whole system.

    Both have PATH, you have to decide if you want to add the program for all users or only for you. Select Path and press the button edit. You will see a list of programs, you can add one manually by pressing New or edit one by pressing Edit.

  4. Adding one easily via explorer by pressing Browse and selecting the location of the bin directory, for example C:\Program Files\kotlin\bin (or whatever directory where you unzipped the kotlin-compiler-$version.zip file).

Now you will be able to run kotlin scripts in any folder by running the next command in the terminal:

kotlin my-script.main.kts # or whatever name you set to the script file.