Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

In my .bashrc file I have:

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then . /etc/bash_completion; fi

I wanted to use [] around it instead of if then fi

I already have

[ -f ~/.bash_aliases ] && source ~/.bash_aliases
[ "${BASH_VERSINFO[0]}" -ge 4 ] && shopt -s autocd
[ -f ~/.git-completion.bash ] && source ~/.git-completion.bash

so I thought maybe I could use

[ [ -f /etc/bash_completion ] && [! shopt -oq posix] ] && . /etc/bash_completion

but I get

-bash: [: -f: binary operator expected
share|improve this question
2  
[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion –  gniourf_gniourf Apr 20 at 17:12
1  
+1 yup that works! –  Michael Durrant Apr 20 at 17:15
1  
Please post, my fellow New Englander ;) –  Michael Durrant Apr 20 at 17:18

2 Answers 2

up vote 4 down vote accepted

You can just chain &&'s, no need to nest conditionals here:

[ -f /etc/bash_completion ] && ! shopt -oq posix && . /etc/bash_completion

works.

share|improve this answer

You mix up two commands. [ is not eqivalent to if. [ is equivalent to test.

if is not even a simple command but a reserved word (like [[), part of a control structure. if ... then ... else ... fi is nearly eqivalent to ... && { ...; true; } || { ... }

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.