0

I have this file:

/Users/MYUSERNAME/asciidots/asciidots/__main__.py

I want to alias this so that it runs as if it was in my current directory. I was thinking something like this:

alias asciidots="OLDCWD=$PWD && cd /Users/MYUSERNAME/asciidots/asciidots/ && python3 __main__.py ALL_ARGUMENTS_FOLLOWING_THE_ALIAS && cd $OLDCWD"

but I don't know of a way to do this.

2
  • Please edit your question to make clear why the answer isn't suitable. Commented May 28, 2021 at 19:39
  • I made a new question, as stated in the comments of the accepted answer. Commented May 28, 2021 at 20:28

1 Answer 1

1

Use a function instead:

asciidots() (
    cd /Users/MYUSERNAME/asciidots/asciidots/ &&
    python3 __main__.py "$@"
)

"$@" expands to the arguments to the function, and here I used ( .. ) instead of { .. } around the function body to make it run in a subshell. The subshell environment has a working directory of its own, so hopping back to the original directory afterwards isn't needed.

(That should work in at least Zsh and Bash, if I'm not mistaken.)

See: In Bash, when to alias, when to script, and when to write a function? (the title says Bash, but most of that is likely relevant to Zsh too.)

5
  • I realize this does answer, but it doesn't work for my situation. It should be like as if I has the entire asciidots/asciidots/ directory was in the cwd. Commented May 23, 2021 at 22:51
  • @CATboardBETA, mm, what do mean exactly? When the Python process is started, the working directory of that subshell is .../asciidots. Of course you could also replicate the OLDCWD=$PWD && cd && python && cd $OLDCWD within a function too, I just don't see why it would matter.
    – ilkkachu
    Commented May 23, 2021 at 22:55
  • No, I mean that it should be as if all of .../asciidots/ was in your directory before running the function. Commented May 23, 2021 at 22:58
  • @CATboardBETA, I really have no idea what "as if all of .../asciidots/ was in your directory" means, and I'm not sure how it should work with an alias but not with a function. With either of the alias or function case, the working directory of the shell is the original one when the alias/function starts, and it's then changed with the cd, just before the python runs.
    – ilkkachu
    Commented May 23, 2021 at 23:05
  • I'm going to make a new question which better fits what I was asking, and I will accept of this as it did answer what I said I guess. Commented May 23, 2021 at 23:09

You must log in to answer this question.

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