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 - 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 Script Examples
For all the Linux distributions, the shell script is like a magic wand that automates the process, saves users time, and increases productivity. This shall scripting tutorial will introduce you to the 25 plus shall scripting examples. But before we move on to the topic of shell scripting examples, l
15+ 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 command in DevOps
DevOps engineers use Linux every day because itâs free, fast, and easy to customize. More importantly, most DevOps tools like Docker, Kubernetes, Ansible, Terraform, and Jenkins are built to run best on Linux. In DevOps, you often work on cloud servers (like AWS, Azure, or Google Cloud), and most of
9 min read
Implementing Directory Management using Shell Script
Directory management constitutes the functions dealing with organization and maintenance of various directories. Directories usually contain files of any type, but this may vary between file systems. The content of a directory does not affect the directory object itself. Some of the directory functi
3 min read
Shell Script To Broadcast A Message
In this article, we are going to see how to broadcast the message using a shell script in Linux. Sending a message to a specified user logged in to the terminal: Firstly, we will create a .sh file using gedit command. This gedit is a powerful text editor in linux which is a default text editor for G
2 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
eval vs source: For Executing Commands Within a Shell Script
Shell scripting is a fundamental skill for anyone working with Unix-like operating systems, offering a powerful way to automate tasks and streamline workflows. Two key commands, "source" and "eval," play distinct roles in the realm of shell scripting. In this article, we delve into the core concepts
8 min read