Skip to content

Why is there a logical operation in bash_profile

Hi, I was taking a look at my bash_profile today to clean it up and noticed this.

[[ -f ~/.bashrc ]] && . ~/.bashrc

can anyone explain to me what that does? Why compare two values for no reason?

8 points9 comments
Open on Reddit
About this post
Posted
Archived
Score
8 · 100% up
Kind
Text
ID
p4e01

Author

1
posts
3
comments
0
media
View profile

9 comments

It's not comparing two values, it's a list operator.

From man bash:

AND and OR lists are sequences of one of more pipelines separated by the && and || control operators, respectively. AND and OR lists are executed with left associativity. An AND list has the form

command1 && command2

command2 is executed if, and only if, command1 returns an exit status of zero.

In this case, the first command is the test to see if ~/.bashrc exists, to avoid an error trying to source a non-existent file.

You'll sometimes see scripts use the OR control to exit on a failure of a test or another command, e.g.

[[ -f ~/somefile ]] || exit 1

If somefile doesn't exist, stop here with an exit status indicating error!

24 points

thanks for that

4 points
unknown author

More fully, its an example of short-circut evaluation which you will see in many programming languages. This is a trick built around an optimization, since you only evaluate far enough to get a final value.

The basic idea is when foo is false, foo && True will always be false and if foo is True, foo || anything will still be True. The trick is that segments in the statement don't get resolved until needed.

So with:

[[ -f ~/.bashrc ]] && . ~/.bashrc

if [[ -f ~/.bashrc ]] is false, there is no point to evaluating the rest of the expression, we know false && anything is false, and the rest of the expression won't be evaluated. If its true, the expression does rely on the . ~/.bashrc part and thus gets evaluated.

Note that [[ is technically (it will be a built in) a command, and the && and || operators operate on the return codes of commands.

Here's an example from the wiki:

int a = 0;
if (a && myfunc(b)) {
    do_something();
}

myfunc will never run. Important gotcha if plan on relying on side-effects in myfunc.

10 points

It's short for

 if [[ -f ~/.bashrc]]; then
    source ~/.bashrc
 fi

"[[ -f ]]" checks if a file (.bashrc in your home, in this case) exists (and is a file, "-d" is for directories).

source .bashrc "loads" .bashrc and everything defined therein into the current environment.

10 points

can someone explain why it uses double brackets instead of one?

3 points
unknown author

What is the difference between double and single square brackets in bash?

For the most part, the rest is just some nicer syntax. To see more differences, I recommend this link to a FAQ answer: What is the difference between test, [ and [[ ?. In fact, if you are serious about bash scripting, I recommend reading the entire wiki, including the FAQ, Pitfalls, and Guide. The test section from the guide section explains these differences as well, and why the author(s) think [[ is a better choice if you don't need to worry about being as portable. The main reasons are:

  1. You don't have to worry about quoting the left hand side of the test so that it actually gets read as a variable.
  1. You don't have to escape less than and greater than "< >" with backslashes in order for them not to get evaluated as input redirection, which can really mess some stuff up by over writing files. This again goes back to [[ being a builtin. If [ (test) is an external program the shell would have to make an exception in the way it evaluates < and > only if /bin/test is being called, which wouldn't really make sense.
5 points
unknown author

I always forget that. And when I look it up its gone again in less than a week.

1 points

I added a comment above that line in mine that says "I don't know what this does. Don't touch it. "

I guess I can change that now. Then maybe learn some bash.

2 points

Keyboard shortcuts

Move and act

jork
Next / previous post or comment
Enteroro
Open the post
f
Open the media viewer
s
Save
h
Hide (the toast has Undo)
Tab
Right after an action: the toast's Undo
x
Collapse the comment
v
Next layout: cards, compact, grid

Go to

/
Search
gthenh
Home
gthena
All
gthens
Subreddits
gthenl
Library
?
This list
Esc
Close a menu or this list

In the viewer

←or→
Previous / next file
↑or↓
Previous / next post
korj
Previous / next post
Space
Play / pause
m
Mute
z
Zoom
s
Save
o
Open the post (on its own page: back to it)
Esc
Close

Keys do nothing while you type in a box.