9

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
3
  • 6
    source doesn't make the script available by name; it just executes each command in the given file in the current shell, as opposed to in a new process. Commented Feb 14, 2020 at 21:04
  • 1
    You could simply add ~/Documents to your PATH variable, in which case you could run marco.sh. If you marco alone to work, you'll need to define an alias, or a function, or create a symlink to ~/Documents/marco.sh in one of your PATH directories, etc. Commented Feb 14, 2020 at 21:05
  • Don't run commands from the Documents directory. Instead, create a ~/bin directory, add it to your PATH near the front, and put the command in there. Commented Feb 14, 2020 at 21:52

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.