1

I apologize in advance, I'm not good at writing shell scripts.

I have a bash function that looks like this:

tagDownload() {
    mp3 "$@" | /home/username/bin/tag.py
}

where mp3 is another bash function that produces some output that is used by the python script. This function is stored in a dotfile that is sourced whenever a terminal is opened.

What I expect (and want) to happen is for tagDownload and its constituent components to only be invoked when called. What is actually happening is that the python script is invoked when the dotfile that houses this definition is sourced when a new terminal is opened.

I have two questions:

  1. How should I structure tagDownload so that when its parent file is sourced, the python program isn't invoked?
  2. What rules does bash use to determine if something is an invocation vs a statement? I've had problems with this before and want to avoid such headaches in the future.

If I'm thinking about this incorrectly, please, correct me.

  • Are you sure you aren't specifically calling tagDownload somewhere later in your dotfile? – jesse_b Oct 25 '19 at 14:21
  • 3
    A function will not be called automatically by defining it. You have a call to tagDownload somewhere, or to the Python script. Use set -x at the top of the file to see what's happening when a terminal is opened. – Kusalananda Oct 25 '19 at 14:40
  • As kindly pointed out by jesse_b and Kusalananda, I was actually calling the function elsewhere in my dotfiles due to a typo :S I feel embarrassed. If either of you posts an answer, I will mark it as the solution, thanks so much! – jakks0 Oct 25 '19 at 15:38
  • Hope to see you hear again. – ctrl-alt-delor Oct 25 '19 at 17:21
0

As discussed in the comments to the question, when defining a function in the shell, the act of defining it does not cause it to be called.

As the user realised later, they were in fact calling the function from another place, which caused the Python script to run.

| improve this answer | |

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.