0

I do not understand the following expression.

function abc(){

..............

...............

[[ -f $filename]] && return 0 || return 1

}

As per tutorial if there is a file exists with filename variable name then this function returns 1 otherwise it returns 0.

I understand && || operator ,but how is this statement getting the desire result?

As per me,In case [[ -f $filename ]] evaluates false ,then one statement of && is false then result of and is false.Now it goes to OR and if first operand is 0 it returns result of second operand so it should return 1,but instead it is returning 0.

How is this being evaluated?

1
  • this is shell, not unix per se. you must misheard something: 0 is "true" and everything else is "false," so the function should return 0 if the file exists, which it does.
    – user601
    Apr 24 '18 at 19:07
2

Both return statements on that last line of the function can be removed.

[[ -f "$filename" ]]

This is the last statement in the function with both return's removed (note the quoted variable expansion and the added space before ]]). The "exit value" of the function will be the result of this statement.

If the file $filename exists, the function will exit with a value of zero (signifying "success", "yes", "ok" etc.), otherwise it will exit with a value of one (or more generally, non-zero, signifying "failure" of some kind).


Don't mix || and && on the same line unless you know what it does. In a command line as

command1 && command2 || command3

the last command would be executed if either of the previous commands failed (returned non-zero).

It's better to write

if command1; then
    command2
else
    command3
fi

if this is what you meant.

This matters in commands like

[[ -f "$filename" ]] && echo "exists" || touch "$filename"

This would try to execute the touch command if the echo failed, which it may do if there's nowhere to output the string to (a write error occurs).

9
  • ok.i was under impression that if [[ -f $filename ]] is able to find file it returns 1 else 0 because i executed on piece of code and it worked fine....if [- f $filename]then echo "File exist" else echo "not exist" fi... Apr 24 '18 at 19:16
  • @rahulsharma As user 'hop' commented on your question, zero means "true". This is because the exit status of functions and utilities in general should be able to report more nuanced error conditions. Look at the manual of rsync, for example and look for "EXIT VALUES" in there.
    – Kusalananda
    Apr 24 '18 at 19:29
  • Thanks.I got now.In case file exists,function abc will return 0.Now in case i call this function abc and with if statement i check :- if ( abc filename ) then do this else do that fi. So if file exists then abc will return 0,but how is if statement is getting succeed in case return value is coming as 0? Apr 26 '18 at 16:00
  • @rahulsharma It's the same thing there. if functionname filename; then dosomething; else somethingelse; fi will execute dosomething if the file exists (the function returns 0), and it will execute somethingelse if the file does not exist (the function returns non-zero).
    – Kusalananda
    Apr 26 '18 at 16:06
  • if function returns 0 ,it means if statement will understand that 0 exit code means success,so dosomething will be evaluated.Am i correct? Apr 26 '18 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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