0

Is there any Python/setuptools approach I could take to write a tool to change directory? There's a somewhat complicated path behind the scenes computed from a single parameter.

e.g., here's the closest I could come (in bash, untested):

alias mycd='function _mycd() { cd `myscript $1` };_mycd'

where myscript is something I can make available using console_scripts in setuptools.

I'm wondering if there was any way I could accomplish this in a self-contained manner, where for example, I don't edit a user's .bash_profile or save something in their bin or whatever. e.g., can I define an alias using setuptools?

amos
  • 5,092
  • 4
  • 34
  • 43
  • The working directory is a property of an individual process, and cannot be changed by a script. `cd` is a shell built-in, and the current working directory of a process is inherited by its children. What is your actual use case? – chepner May 25 '18 at 18:47
  • Right... so I'm wondering if there's any way to create a "cd `myscript $1`" for the user? Power users could just create the alias themselves, I'm just wondering if I could automate that. – amos May 25 '18 at 19:05
  • you might want to use the ```os``` package. [copy](https://stackoverflow.com/questions/431684/how-do-i-change-directory-cd-in-python)? – lwileczek May 25 '18 at 19:11
  • Could you not do `alias mycd='function _mycd() { cd "$(/full/path/to/myscript $1)" };_mycd'`? Changed backticks to `$()`. Couldn't figure out how to print those. \` \` I get it now =D. – Jason May 25 '18 at 19:17
  • 1
    I'm afraid the best you can do is provide instructions on how to edit the user's relevant shell configuration files. – chepner May 25 '18 at 19:18
  • 1
    When you do, skip this odd alias no-op and just suggest `mycd() { cd "$(myscript "$1")"; }` – that other guy May 25 '18 at 19:52
  • I'm not convinced that there's no possibility given this context. E.g., when I'm picking my `conda` environment, I use `source activate myenv` where `source` is a script run in the current shell and could perhaps define an alias/function. I take it there's no way to hook into that, but it seems like there could be in principle at least. Maybe this isn't enough to clear the duplicate flag on this post though. – amos May 29 '18 at 13:06

0 Answers0