Shell Scripting – Umask Command
Last Updated :
25 Apr, 2022
A Shell script is a plain text file. This file contains different commands for step-by-step execution. These commands can be written directly into the command line but from a re-usability perceptive it is useful to store all of the inter-related commands for a specific task in a single file. We can use that file for executing the set of commands one or more times as per our requirements.
The umask command is used to change the default file permission of newly created files.
The syntax for the Umask command is –
umask [ value ]
The value here is used to specify the file permissions. Like the value of 444 in a Linux file permission means that everyone can only read the file. Now we will subtract this value from 777 to get the umask value.
Note: To know more about this it is recommended to go through the following article – Umask command in Linux with examples. In this article, we are going to discuss Umask in the context of Shell scripting.
Let’s understand using examples –
Script with Umask for changing File Permission
# the owner both read and write,
# everybody else can only read them
umask 022
# prints the new umask value
umask
# creates a new file
touch my_gfg_notes.txt
# shows the newly created files permission
ls -l my_gfg_notes.txt
Output
0022
-rw-r–r– 1 satyajit satyajit 0 Apr 6 16:04 my_gfg_notes.txt
Below is the terminal shell pictorial depiction after executing the following script –
Here in this example, we have changed the umask value first to 022, which means that the owner can both read and write all newly created files, but everybody else can only read them. After that, we checked whether the umask value is updated properly or not, and then we created a new text file and checked its default permission. The output states the file permission as -rw-r–r–, which indicates the script using umask properly updated the default file permission settings.
Use of umask command from If Statements in a Script
PER=”OWNER_READ_WRITE”
if [ “$PER” == “OWNER_READ_WRITE” ]; then
umask 077
echo “Files will, by default, be unreadable by anyone else on the system except user”
else
umask 022
echo “The owner can both read and write all newly created files, but everybody else can only read them”
fi
touch my_gfg_notes.txt #creates a new file
ls -l my_gfg_notes.txt #shows the newly created files permission
Output
Files will, by default, be unreadable by anyone else on the system except user
-rw——- 1 satyajit satyajit 0 Apr 6 16:37 my_gfg_notes.txt
Below is the terminal shell pictorial depiction after executing the following script –
Here in this example, we have used if statements to determine the umask value in a shell script. After that like the earlier one, we created a new file and showed its file permissions.
Use of umask command from Switch Statements in a Script
preference="A"
case "$preference" in
# case 1
"A") umask 077
echo "no permission";;
# case 2
"B") umask 022
echo "only owner can write";;
esac
touch my_gfg_notes.txt
ls -l my_gfg_notes.txt
Output
no permission
-rw——- 1 satyajit satyajit 0 Apr 8 11:00 my_gfg_notes.txt
Here in this example, we have used switch statements to determine the umask value in a shell script. After that like the previous ones we created a new file and showed its file permissions. Below is the terminal shell pictorial depiction after executing the following script –

Similar Reads
Shell Scripting - Set Command
The `set` command in shell scripting is a powerful tool that used for controlling the behavior of the shell and the environment in which scripts run. It allows the users to modify the shell options and positional parameters which facilitates providing greater control over script execution and debugg
7 min read
Shell Scripting - Readonly Command
In this tutorial, we will learn about the âreadonlyâ command. Using the readonly command, you can assign a particular value to a variable that can neither be changed nor modified by subsequent assignments nor be unset. Syntax:readonly name[=value] readonly [-aAf] [name[=value] Most used options: -p
3 min read
Shell Scripting - Subshell
Shell scripting is a powerful tool for automating tasks and simplifying the management of systems and applications. One important concept in shell scripting is the use of subshells, which allow you to execute commands within a separate shell environment. A subshell is a child shell that is spawned b
4 min read
Shell Scripting - Restricted Shell
Shell is one of the most important and powerful tools available in GNU/Linux-based systems. One can control the entire system if used correctly, this exact feature makes the shell a double-edged sword. It can potentially harm the system if one executes a system command without knowing its underlying
5 min read
fuser command in Linux
fuser is a command line utility in Linux. fuser can identify the process using the files or sockets. This command can be also used to kill the process. In this article, we are going to see how to use the fuser command to list the processes and kill the processes associated with files and directories
4 min read
Umask command in Linux with examples
The umask command in Linux is used to set default permissions for files or directories the user creates. How does the umask command work?The umask command specifies the permissions that the user does not want to be given out to the newly created file or directory.umask works by doing a Bitwise AND w
10 min read
Shell Scripting - Creating a Binary file
While working in Linux systems, we have used so many commands on a day-to-day basis. Most of the commands are in the binary format resides under /bin, /sbin, /usr/bin, /usr/sbin, /usr/local/bin, etc directories. As system administrators, we would have to write many shell scripts to do a few tasks or
4 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
7 min read
Unzip Command in Linux
As an open-source operating system, Linux presents plenty of powerful and versatile instructions for dealing with files and directories. One such command that performs an important role in coping with compressed files is the "unzip" command. Compressed files are a common way to keep space and share
8 min read
Linux Security Command Cheat Sheet
Maintaining a secure and hardened Linux system is crucial in today's threat-laden digital landscape. This comprehensive Linux Security Command Cheat Sheet serves as an invaluable resource for system administrators and security professionals, providing a concise reference to essential commands spanni
7 min read