Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is it possible execute chmod and ignore error from command

Example ( remark file.txt not exsist to show the example )

When I type

chmod 777 file.txt

I get error on the output

  chmod: cannot access file.txt : no such file or directory

So I add the-f flag to the command as the following: ( file.txt not exist in order to show the case )

  chmod -f 777 file.txt
  echo $?
  1

But from the example chmod return 1

Please advice how to force the chmod command to give exit code 0 in spite of error

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Please advice how to force the chmod command to give exit code 0 in spite of error

chmod -f 777 file.txt || :

This would execute :, i.e. the null command, if chmod fails. Since the null command does nothing but always succeeds, you would see an exit code of 0.

share|improve this answer
    
Equivalent, but more readable for a casual user is to use chmod -f 777 file.txt || true –  orion Mar 5 at 11:03
    
@orion Depends. If one is familiar, then : seems equally readable. –  devnull Mar 5 at 11:04

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.