zhuguowei
zhuguowei

Reputation: 8487

How can zsh and normal shell share environment variables and aliases without copying each other

Right now I tried to use zsh from normal Ubuntu bash. When I changed to zsh shell, I found previously environment variables (e.g. JAVA_HOME) in .bashrc can not migrate to .zshrc automatically. Now I just copy them (export, alias in .bashrc) to .zshrc. I want to know is there other convenient way to share these thing in .bashrc and do not need copy them explicitly? And even when I add something in .zshrc and then change to normal bash still could share them in .zshrc without copy them to .bashrc.

I tried to source .zshrc in .bashrc, then change to bash, found below error

exec bash
autoload: command not found
bash: /home/zhuguowei/.oh-my-zsh/oh-my-zsh.sh: line 31: syntax error near unexpected token `('
bash: /home/zhuguowei/.oh-my-zsh/oh-my-zsh.sh: line 31: `for config_file ($ZSH/lib/*.zsh); do'

And in .zshrc I also tried source .bashrc, have error too

source .zshrc 
/home/zhuguowei/.bashrc:16: command not found: shopt
/home/zhuguowei/.bashrc:24: command not found: shopt
/home/zhuguowei/.bashrc:108: command not found: shopt
/usr/share/bash-completion/bash_completion:35: parse error near `]]'
\[\e]0;\u@\h: \w\a\]\u@\h:\w$ 

Upvotes: 4

Views: 3664

Answers (2)

bew
bew

Reputation: 503

In .bashrc you can use export to export a variable (usually in UPPER_CASE) to the environnement that will be sent to commands executed from your shell.

Example of a simple .bashrc

# Here is the content of the .bashrc

export SOMETHING=42

Now in bash, after sourcing the bashrc, I have an environnement variable called SOMETHING that contains 42

You can check what is the environnement beeing sent to process with the comand env

Now, in the opened bash, you can launch zsh, then check (with env) the zsh's current environnement.

Now in the opened zsh, you can just echo $SOMETHING and see the answer 42

note: if you don't know why I used 42 : (wikipedia)

Upvotes: 1

mauro
mauro

Reputation: 5940

  • I would (hard) link .bashrc & .zshrc

OR

  • Source one of the two files from the other.

Upvotes: 0

Related Questions