I have a shell script that creates a function (in a bashscript file which is sourced by .bashrc) for each Markdown files to open it in the editor.


ls /home/nikhil/Notes/Studies

ls /home/nikhil/Notes/Sports

As you can see, some folder (such as Studies) contains a file with the same name (Studies.md) and index.md, while others (like Sports) contain index.md but not Sports.md

Now, my bashscript creates following functions

oPhysics(){ ${Editor:-vim} /home/nikhil/Notes/Studies/Physics.md }
oChemistry(){ ${Editor:-vim} /home/nikhil/Notes/Studies/Chemistry.md }
oStudies(){ ${Editor:-vim} /home/nikhil/Notes/Studies/Studies.md }
oiStudies(){ ${Editor:-vim} /home/nikhil/Notes/Studies/index.md }

oFootball(){ ${Editor:-vim} /home/nikhil/Notes/Sports/Football.md}
oCricket(){ ${Editor:-vim} /home/nikhil/Notes/Sports/Cricket.md}
oiSports(){ ${Editor:-vim} /home/nikhil/Notes/Sports/index.md}

Notice that: oStudies and oiStudies exist, while only oiSports exists.


Now, as a user, I would like to type oStudies or oSports (this would fail) and not oiSports, and it should automatically run oiSports when oSports function does not exists. This is because, most of the time the user is interested in opening the normal markdown files, and only when they don't exist the user would like to open index.md. The user does not remembers for which folder only index.md exists (for example Sports folder).

How can I do this? The pattern I am looking for is to run a function oiBlahBlah if oBlahBlah does not exists, given that oBlahBlah is invoked by the user.

Replies to suggestions

Making alias won't work as I don't want oStudies to execute oiStudies

  • Make an alias? alias oSports=oiSports Commented Jan 24, 2020 at 19:16
  • @StephenHarris: Making alias won't work as I dont want oStudies to execute oiStudies.
    – Porcupine
    Commented Jan 24, 2020 at 19:42
  • 2
    Just to comment: EDITOR is a fairly standard environment variable that you could use instead of Editor. Many programs use it unless VISUAL is set to some program. Just using "$EDITOR" in your functions and then set+export that variable in your shell's startup files would make the functions look cleaner.
    – Kusalananda
    Commented Jan 24, 2020 at 20:05

2 Answers 2


An answer to a different problem. If the user wants to edit the main markdown file and only wants to edit the index file if it doesn't exist, why not write the function to do that? Then just have the oSports function. Refactoring to have the common code in a function.

# Internal edit markdown function
   if [ -e "/home/nikhil/Notes/$1/$2.md" ]
       ${Editor:-vim} "/home/nikhil/Notes/$1/$2.md"
       ${Editor:-vim} "/home/nikhil/Notes/$1/index.md"
oPhysics(){ _emd Studies Physics ; }
oChemistry(){ _emd Studies Chemistry ; }
oStudies(){ _emd Studies Studies ; }
oiStudies(){ _emd Studies index ; }
oFootball(){ _emd Sports Football ; }
oCricket(){ _emd Sports Cricket ; }
oSports(){ _emd Sports Sports ; }
oiSports(){ _emd Sports index ; }

The circumstances not obvious, but a simple solution could be:

First define every functions like this:

oStudies() { oiStudies; }

etc. then let your script override these functions where needed.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .