Understanding created file permissions

When Bash creates a new file on the system for you into which output has been directed, it needs to decide what permissions to set for the new file. It decides what those permissions should be with reference to a value called umask for the current process, which can be set by the process' owner.

The logic for how this decision is made is a little involved, and may require a quick review of how permissions on Unix-like systems work. To start, let's create a new file using an output redirection, and examine it with ls -l:

$ printf 'Hello, world\n' > myfile $ ls -l myfile -rw-r--r-- 1 bashuser bashuser 1 2018-07-29 20:53:23 myfile

 

The permissions for the file are described in the the -rw-r--r-- string at the start of each line. This is a human-readable version of the permissions; each character is either a letter flag or a dash. It's possible that your own value may differ, depending on your system's default settings:

If we have GNU stat, we can also examine the permissions in another, more compact, format; a numeric notation with three octal numbers ranging from 0 to 7:

$ stat -c %a myfile
644

This value is calculated by adding 4 for each read bit if set, 2 for each write bit if set, and 1 for each execute bit if set:

If we were to make the file executable by the owner, the owner permissions would add 1, and would then be 7, for numeric permissions of 744.

This numeric format is not as easy for humans to read as the symbolic format, but it is closer to how the computer represents the permissions for files.