4

I found this command. I understand the source reloads the bash profile and fixed permissions for Ruby permissions needed on Ubuntu. But what does the part before && mean?

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" 
mu is too short
  • 426,620
  • 70
  • 833
  • 800

3 Answers3

6

Let's break it down:

the [[ and ]] enables advanced functionality that is more similar to traditional programming languages, allowing constructs like || instead of -o and && instead of -a.

For more information on this see How to use double or single brackets, parentheses, curly braces but as far as I know -s does not need the extra functionality and the double brackets could be replaced with single brackets)

the -s tests whether the file exists and is non-empty

&& will automatically run the command on the right, as long as the command on the left executes without an error return code.

So, the statement tests whether the file exists and is non-empty, and if so it will source it into the current shell environment.

Community
  • 1
  • 1
DavidDraughn
  • 1,110
  • 1
  • 8
  • 15
3

The [[ ... ]] part allows to test a condition using operators. Think of it as an if statement. In your example, you're using the -s operator, which tests that the referenced file is not empty.

So, essentially, the command you posted sources the rvm file if it's not empty

Dia Kharrat
  • 5,948
  • 3
  • 32
  • 43
  • 2
    Although it should be single square brackets, not double. No reason to use a bash-ism when the standard form is shorter. – Nemo Oct 13 '12 at 00:33
  • Very nice answers guys. I couldn't even find this notation in either of my Red Hat RHCSA books. –  Oct 13 '12 at 13:49
1

[[ is the bash built-in test function. -s returns true if the following file has size greater than zero. See man bash (Conditional Constructs). Scroll down a little to [[...]]

Adrian Pronk
  • 13,486
  • 7
  • 36
  • 60