Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

[ "$2" = "" ] && >$1 && return
share|improve this question
add comment

1 Answer 1

up vote 5 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.