Julia's shell mode can't recognize user-defined aliases of system commands on Linux

Why can’t Julia’s shell mode recognize user-defined aliases of system commands on Linux?

I defined some aliases in the ~/.bashrc file such as

alias mydir="cd ~/path/to/my/directory/"

so that I can conveniently cd to my directory.

But in the Julia REPL when I typed ; to enter the shell mode I found all the aliases I defined in the system .bashrc file no longer work.

What’s the reason? And how to make them work?

Thanks!

they don’t work because julia’s shell mode (just like it’s run command) doesn’t execute inside bash,
It doesn’t execute in a shell at all.

3 Likes

I don’t think there is any easy way to make them work.
Especially for something which changes global state like cd.
But I could be wrong there.

For some things you can get them to work by doing bash -e mycommand etc.
This doesn’t work for cd since it runs in a seperate process with its own current directory.

Instead I suggest using a julia function
mydir() = cd(expanduser("~/path/to/my/directory/"))
which you can define in your .juliarc
which you can just call in julia mode

Thanks! But how can I know which commands can work within Julia’s shell mode? I found I can even run julia again and again within an opened Julia’s shell mode if I have added its installation path to the environment variable in .bashrc. What is this feature and Julia’s shell mode designed for?

I think I had the same issue and fixed it by putting the following line in .bash_profile:

source .bashrc

Not sure it’s very elegant but it worked for me.
My problem was getting Julia to see my global mamba installation for Python.

1 Like

Thanks. Don’t know why it still didn’t work for me. :face_with_diagonal_mouth:

Do you mean I should define the aliases in .bashrc first and then add source .bashrc to .bash_profile? If so it didn’t work for me.

1 Like

That’s because julia is an actual binary program that can be invoked, and the julia process you start from your shell inherits the environment variables currently set in your shell.

Julias shell> prompt can only spawn processes - things like shell aliases are features of your particular shell (bash, zsh, etc), not of “spawning a process” in general. The same is true for other shell-builtins like ulimit or export in bash; they are not accessible to the shell> prompt because they’re not actual programs that can be invoked.

You can think of your shell as a sort-of mini programming language that’s interpreting space-separated commands as “spawn a process” or similar, so just like you can’t use zsh builtins in bash, you can’t use bash builtins (or aliases) in shell>.

1 Like

Thanks for your explanation about its underlying mechanism. I seemed to understand. When I first learnt of the shell mode of Julia I thought I could do anything under Julia’s framework without having to exit it and now it seems I wanted too much.

You could start any shell though. Occasionally, I run shell> fish to have access to my aliases, functions etc. without leaving the Julia REPL.

This starts a subshell (check env and you see SHLVL set to 2). You can exit that subshell with ^D to go back to the shell> mode.

Just be aware that any shell variables you set will be lost after exiting i.e., if you start shell> bash, set something like important_var=42, exit, and start shell> bash again, the variable will be gone.

2 Likes

Nice! :+1:

For completeness, the thing I tested in the Julia REPL is access to environment variables ($PATH) defined in .bash_profile, and that worked. So it’s another issue than aliases

1 Like

:handshake: :handshake: