0

I've a shell script that updates the proxy settings on my system.

I tried running the command chmod +x filename.sh which ran successfully. And then I ran ./filename.sh on the terminal that also ran successfully. This shell script contains some helper methods which when I am trying to run it says -bash: unload_proxy: command not found. enter image description here

enter image description here

  • 5
    You don't have the unload_proxy command. It says it's a function so you need to find where (if anywhere) that function is stored and source that file in order to have access to it. Where did this shell script come from and did it come with instructions? Also please don't post screenshots of text, just copy and paste it directly from your terminal into the question. Finally you have both the linux and macos tags, which is it? – jesse_b Jan 30 at 14:39
  • Hi @jesse_b Sorry for the confusion, I was trying to run this script on a Mac terminal, this shell script was developed by one of our team members. – Karan Bhatia Jan 30 at 17:47
2

If filename.sh contains the definition of unload_proxy, then you need to use source ./filename.sh or the shortcut . ./filename.sh.

Sourcing the file causes it to evaluate within the current shell's environment, thereby letting it define new functions.

in your example, you're running the file directly* (ie without sourcing), and the sequence of events is as follows:

  1. a new bash shell is created, with a new environment that is, for the purposes of this explanation, more or less a copy of the current shell's (dependent on certain options and ways of defining things).

  2. the script is run inside this new shell, and defines the functions within its environment.

  3. the script ends, and the new shell with the functions defined in it ends as well.

  4. control is returned to your original shell, where no functions have been defined.

When run as source ./filename.sh, no new shell is launched, so only step number 2 happens, and it happens in your current shell.

* assuming the file has a proper shebang, such as #!/usr/bin/env bash

| improve this answer | |
  • Hi @codelahoma thanks for the help, the issue was exactly with sourcing the file. – Karan Bhatia Jan 30 at 17:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.