Question

I have the following in a file which is sourced both by .bashrc and .zshrc. The syntax is from the site.

if $SHELL=/bin/zsh                                                               
         then
     # to not java .class when running java 
             myclasslist () { reply=(${$(ls *.class)%.class}) }
             compctl -K myclasslist java
fi

Both Zsh and Bash go upset of it.

How can you make a if -clause in sh?

Was it helpful?

Solution

You need to wrap your condition in the test operator (as well as quotes to get in the habit):

In shell:

#!/bin/sh
if [ "$SHELL" = "/bin/zsh" ]; then
       # do your thing
fi

In bash:

#!/bin/bash
if [ "$SHELL" == "/bin/zsh" ]; then
    # just do it!
fi

OTHER TIPS

You need the test alias [ command:

if [ "$SHELL" = "/bin/zsh" ]
then
    myclasslist () { reply=(${$(ls *.class)%.class}) }
    compctl -K myclasslist java
fi

On the web site you mention, they do not use the test operator ([]) because they say:

For example, cat returns 0 if it runs successfully, and 1 if it encounters an error

The if must be followed by a "boolean statement" and if this statement returns 0, the "then" is actually executed...

In your case, Seth's answer is what you're looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top