1

Lets say I have this bash script (test):

#!/usr/bin/env bash
source ~/.zshrc

In my .zshrc, I have the following:

autoload -U compinit
compinit

When I try and run 'bash test' from my terminal window (zsh), I get errors saying autoload and compinit commands are not found. If I just do source ~/.zshrc from the command line, it works fine.

I am trying to setup my development environment, similar to this blog, but when the scripts try and source the .zshrc file it fails.

Any insight would be appreciated.

phuclv
  • 37,963
  • 15
  • 156
  • 475
kelsmj
  • 1,183
  • 11
  • 18
  • Why aren't you doing a zsh script if you need your zsh profile? – Mat Jan 15 '12 at 17:14
  • Well, I guess the state of my confusion starts with the blog article, and they have a bash script that seems to me they are running in a zsh shell....I will try the suggestions here... – kelsmj Jan 15 '12 at 17:30
  • I don't see anything in that blog article about `source`ing your .zshrc from any sort of script, let alone bash scripts. – Gordon Davisson Jan 15 '12 at 21:00
  • If you look at the [mac](https://github.com/thoughtbot/laptop/blob/master/mac) script in the laptop repository, it is doing it there. In the blog, they switch to zsh shell, then have you run that script to setup your mac environment. – kelsmj Jan 20 '12 at 12:44
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww May 09 '18 at 04:02

2 Answers2

8

In your script, you're using bash to run a zsh script. You might as well ask the python interpreter to parse perl.

Either change bash to zsh in the shebang line or write the script with bash commands.

Kevin
  • 53,822
  • 15
  • 101
  • 132
2

It's not quite as bad as python vs. perl. Both bash and zsh are derived from the Bourne shell (whose behavior is standardized by POSIX), so any script designed to work with /bin/sh is likely to work with either bash or zsh.

Normally your ~/.zshrc, as the name implies, is designed to be used with zsh, and will typically include zsh-specific commands like autoload and compinit.

You can make those commands conditional, for example:

if [ "$ZSH_VERSION" ] ; then
    autoload -U compinit
    compinit
fi

But of course that means you won't get the functionality of those commands, unless you can figure out a way to emulate them in bash. (I'm not familiar with either command, so I can't help you there.)

(Note that this will fail if you've done set -u or set -o nounset in your bash shell.)

But if you're going to be using both zsh and bash, it probably makes a lot more sense to have separate ~/.bashrc and ~/.zshrc files, and use each one only with the shell for which it's designed. If you want to avoid duplication, each one can source a third file containing common commands.

(And based on the comments, it's likely you're doing the wrong thing in the first place.)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • I kind of gave up with the walk through I was using, and just made my own bash script to setup my laptop. The code I was basing my initial script on is overkill for me anyway. Thanks for the input. – kelsmj Jan 16 '12 at 02:23