Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can anyone help me understand the meaning of this line from a bash script?

[ "$2" = "" ] && >$1 && return
share|improve this question
up vote 6 down vote accepted

&& is the “and” operator: it executes the left-hand command, and if that command is true (i.e. exits with a status of 0) it then executes the right-hand command.

[ "$2" = "" ] tests whether the second parameter to the enclosing function (or script, if used outside a function) is empty. (If there were fewer than two parameters, $2 is empty.)

>$1 is the empty command redirected to the file whose name is the first parameter $1. This creates an empty file if it doesn't exist, and empties the file if it already exists. This command can fail if the script doesn't have the permission to create or modify the file.

return exits the enclosing function.

Thus the meaning of this line is: if $2 is empty, create or truncate the file named $1, and if that succeeds, return from the function.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .