Bash – How to use the “source” command in Zsh

bashmacosterminalzsh

I'm working inside a Zsh terminal on a Mac.

I'm trying to use the source command so that I can just call my script by name, rather than typing the path to the ".sh" script. The source command does not return any errors, but once I try calling the ".sh" file by name it returns "command not found."

I've also tried typing in the absolute path when using the source command, but with no luck.

Terminal commands:

source ~/Documents/marco.sh
marco
zsh: command not found: marco

marco.sh

#!/usr/bin/env bash  
touch ~/Documents/marco.txt
echo $(pwd) > ~/Documents/marco.txt

Best Answer

To do it exactly the way you want to, you can make a marco function:

# marco.sh
marco () {
  touch ~/Documents/marco.txt
  echo $(pwd) > ~/Documents/marco.txt
}

Then you source it and run marco. If you're going to use this a lot, I suggest putting the function in .zshrc or another file that will be sourced by your shell automatically.

And as suggested in comments, you could also put your original marco.sh in your path. I like to use ~/bin for these types of personal executables:

$ mkdir ~/bin
$ mv ~/Documents/marco.sh ~/bin
$ export PATH="$HOME/bin:$PATH"
$ marco.sh

Again, put the export PATH line in your .zshrc or similar file.