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:
- The first character of the string (-) is not a permission flag; it just shows the file type. In this case, a normal file (not a directory) shows a hyphen (-) in this field. A directory would show d instead.
- The next three characters (rw-) are the file owner permissions: read, write, and execute. Since you have just created the file, its owner will generally be you. In this example, the owner is able to read from and write to the file, but not to execute it as a program.
- The next three characters (r--) are the file group permissions: what a user in the file's group can do with the file if they're not the owner. In this case, members of our group can read the file, but can neither write to it nor execute it as a program.
- The last three characters (r--) are the permissions for the world, or any other users who are neither the file's owner, nor in its group. Again, these users can read our file, but cannot write to it or execute it.
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:
- The owner has read and write privileges, so the first value is 4 + 2, or 6
- The group has read privileges only, so the second value is just 4
- The world has read privileges only, so the third value is just 4
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.