简体   繁体   中英

what does [[ -s mean in linux shell

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" 

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

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.

[[ 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 [[...]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM