cmd1 && cmd2
Runs cmd2 if and only if cmd1 is successful. So cmd1 && cmd2
is successful if both cmd1
and cmd2
are successful which is the idea for a "AND" operator.
Here, cmd1 is the [
command (used to perform tests) and cmd2 is the .
command (used to tell the shell to evaluate the code contained in a given file).
For the [
command -s file
is to check whether the file
file has a non-zero size (and return success if that's the case).
So that command line means: interpret the code in the $NVM_DIR/nvm.sh
file if that file is not empty.
If it was empty, that would not be a problem as the .
command would just do nothing. That test is still useful though as the [
command will also return false if the file doesn't exist. In Bourne-like shells (not bash
though unless in POSIX conformance mode), doing . a-file-that-does-not-exist
would cause the shell to exit (and even in bash
that would cause an error message to be displayed).
So if that file is not guaranteed to exist (and it's OK if not), it's a good idea to check for that beforehand, and [ -s file ]
is one way to do it. It also has the benefit to skip the .
command if the file is empty.
Note that [ -s file ]
will also return true/success if file
is non-empty and not a regular file (like a directory) which would also cause the .
command to fail. Another method could have been [ -f file ]
which tests that file
exists and is a regular file (or symlink to regular file), though that means that one cannot use non-regular files (like fifos) any longer, and one could argue that outputting an error message if the file is a directory would be preferable as that would clearly be a pathological case.
Another approach could be [ -r file ]
to check whether the file exists and is readable. But then again, if it's not, you'd probably prefer an error message to tell you of the problem.
There's also [ -e file ]
to only check if the file exists (regardless of whether it's empty, regular, readable or not).
foo=bar
, then$foo
will not be set after the script exits. If you source it instead,$foo
will be set in the parent shell. – terdon Nov 22 '15 at 14:30source
is a synonym of.
in bash, but.
is more portable. – terdon Nov 22 '15 at 19:18help
can be really minimalist: inhelp .
it is said "Execute commands from a file in the current shell" and not "Execute a file with commands". Each word is heavy :) but useful. You can check the bash equivalence withhelp .
andhelp source
that usually return exactly the same text. – Hastur Nov 23 '15 at 09:34