4

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$ 
Joshua Goldberg
  • 5,059
  • 2
  • 34
  • 39
zhuguowei
  • 8,401
  • 16
  • 70
  • 106
  • One option ought to be to source the .zshrc file from within the .bashrc file. – user2672165 Jan 02 '16 at 14:05
  • Hell no, sourcing rc of an incompatible shell is one of the stupidest suggestions ever (and unfortunately I see it suggested a lot). Write a base POSIX sh-compatible env file, and source it from `~/.zshenv`, `~/.bashrc` and `~/.bash_profile`. Leave everything else shell-specific. You may want to have a look at my setup: https://github.com/zmwangx/dotfiles/blob/master/env, along with bash and zsh runcoms in their own respective subdirectories. I'm not advertising my setup as the best there is, but at least there's no breakage, and I can kickstart any other POSIX shell with the same base env. – 4ae1e1 Jan 03 '16 at 05:29
  • Thanks @4ae1e1 finally I decided to adopt this way : http://stackoverflow.com/questions/764600/how-can-you-export-your-bashrc-to-zshrc – zhuguowei Jan 04 '16 at 03:16

2 Answers2

1

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)

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
bew
  • 483
  • 4
  • 9
0
  • I would (hard) link .bashrc & .zshrc

OR

  • Source one of the two files from the other.
ipinak
  • 5,739
  • 3
  • 23
  • 41
mauro
  • 5,730
  • 2
  • 26
  • 25
  • I tried your way , in `.bashrc` source `.zshrc`, then changed to bash, there are 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' – zhuguowei Jan 03 '16 at 03:51
  • This means your `.zshrc` contains both variable setting (like JAVA_HOME in your original post) and code. The latter could be specific to either bash or zsh. I'd suggest to separate variable setting and code. A file containing variable settings can be _sourced_ by both `.zshrc` and `.bashrc` – mauro Jan 03 '16 at 05:34
  • See also https://stackoverflow.com/a/26020688/411282 which suggests putting shared parts in `.profile` – Joshua Goldberg Sep 14 '17 at 15:28