0

I have two files that I would like to call from any one of multiple working directories on my computer, so I added them to a folder of resources that I've added to my path (and then sourced my .bash_profile and confirmed with echo $PATH).

One, a Python script, I had no issue with. which myscript.py finds and prints the full path of myscript.py

The other is a Tex file, and which mytex.py returns nothing. Other text files in that folder have the same result, and I think it has to do with them being non-executable files.

Is there a way to add the Tex and text files so that they can be called from other directories without typing the full path?

2 Answers 2

0

The PATH is strictly for finding executable programs. It makes no sense to invoke a text file like a program and expect it to launch.

I'd suggest using bash's "command not found" mechanism to search for the document in a distinct search path: add this to your ~/.bashrc

function command_not_found_handle {
    for dir in /search/path/A /search/path/B ... ; do
        if [[ -r "$dir/$1" ]]; then
            open "$dir/$1" "$@"
            return
        fi
    done
    echo "$1: command not found" >&2
    return 127
}

Then, source ~/.bashrc and running mytex.py should launch the file with the default application if the file resides in one of the directories listed in the for command.

2
  • Thanks for this code. I realize my question wasn't clear, but I'm not actually trying to launch a text file. I'm trying to provide a file for pandoc's listings option, i.e. pandoc example.md --listings -H mylistings.tex -o example.pdf example.md might be in my current working directory, but I hope to use this command with mylistings.tex to convert Markdown files in multiple directories. Do you think it would be better to try something within pandoc itself? (Sorry, this just occurred to me.)
    – user225457
    Jul 3, 2015 at 19:20
  • This is why it's important to actually ask your real question instead of trying to abstract it to something else. I'd recommend you ask a new question. Jul 3, 2015 at 20:10
0

Let start with which. The manual states:

    The which utility takes a list of command names and searches the
    path for each executable file that would be run had these commands
    actually been invoked.

Since your text file is not executable (invoked on its own) then which will not list it. A non-executable file within your path can be invoked with an interpreter. Convention would be to cd (change directory) to the folder in which your non-executable file resides then call it something like:

    python mytext.py
2
  • 1
    But that helps him not at all with find the file in some search path. Jul 3, 2015 at 19:07
  • Thanks - I hadn't realized that which only works for executable files. However, I'd still like to know if there's a way to reference that file without the full path or changing my working directory - I'm using that file in conjunction with others in the working directory which I do not want to be added to my path.
    – user225457
    Jul 3, 2015 at 19:11

You must log in to answer this question.