Linux System Programming
Linux System Programming
Unix is also an operating system like Linux. It is a commercial OS. It consists of three parts: Kernel, Shell
and Programs. Most of the Unix and Linux commands are similar in nature.
This course includes all topics of Linux OS such as Linux commands, Directories, Files, Man Pages, File
Contents, File Permissions, shells, VI editor etc. There is also given Linux interview questions to help you
better understand the Linux operating system.
What Is Linux
Linux is an open-source operating system like other operating systems such as Microsoft Windows
, Apple Mac OS, iOS, Google android, etc. An operating system is a software that enables the communication
between computer hardware and software. It conveys input to get processed by the processor and brings output
to the hardware to display it. This is the basic function of an operating system. Although it performs many other
important tasks, let's not talk about that.
Linux is around us since the mid-90s. It can be used from wristwatches to supercomputers. It is
everywhere in our phones, laptops, PCs, cars and even in refrigerators. It is very much famous among
developers and normal computer users.
Evolution of Linux OS
The Linux OS
was developed by Linus Torvalds in 1991, which sprouted as an idea to improve the UNIX OS. He suggested
improvements but was rejected by UNIX designers. Therefore, he thought of launching an OS, designed in a way
that could be modified by its users.
Nowadays, Linux is the fastest-growing OS. It is used from phones to supercomputers by almost all
major hardware devices.
2) System Libraries
System libraries are special programs that help in accessing the kernel's features. A kernel has to be
triggered to perform a task, and this triggering is done by the applications. But applications must know
how to place a system call because each kernel has a different set of system calls. Programmers have
developed a standard library of procedures to communicate with the kernel. Each operating system
supports these standards, and then these are transferred to system calls for that operating system.
The most well-known system library for Linux is Glibc (GNU C library).
3) System Tools
Linux OS has a set of utility tools, which are usually simple commands. It is a software which GNU project
has written and publish under their open source license so that software is freely available to everyone.
With the help of commands, you can access your files, edit and manipulate data in your directories or
files, change the location of files, or anything.
4) Development Tools
With the above three components, your OS is running and working. But to update your system, you
have additional tools and libraries. These additional tools and libraries are written by the programmers
and are called toolchain. A toolchain is a vital development tool used by the developers to produce a
working application.
Some examples of end tools are graphic design tools, office suites, browsers, multimedia players, etc.
systems that make it completely different and one of the most used operating systems. Linux may be a perfect
operating system if you want to get rid of viruses, malware, slowdowns, crashes, costly repairs, and many more.
Further, it provides various advantages over other operating systems
, and we don't have to pay for it. Let's have a look at some of its special features that will attract you to switch
your operating system.
On the other hand, open-source is completely different. The source code is included with the compiled
version and allows modification by anyone having some knowledge. It gives us the freedom to run the
program, freedom to change the code according to our use, freedom to redistribute its copies, and
freedom to distribute copies, which are modified by us.
In short, Linux is an operating system that is "for the people, by the people."
It is secure
Linux supports various security options that will save you from viruses, malware, slowdowns, crashes.
Further, it will keep your data protected. Its security feature is the main reason that it is the most
favorable option for developers. It is not completely safe, but it is less vulnerable than others. Each
application needs to authorize by the admin user. The virus cannot be executed until the administrator
provides the access password. Linux systems do not require any antivirus program.
/C++
, Java
, Python
, Ruby
, and more. Further, it facilitates with a vast range of useful applications for development.
Developers find that the Linux terminal is much better than the Windows command line, So, they prefer
terminal over the Windows command line. The package manager on Linux system helps programmers
to understand how things are done. Bash scripting
is also a functional feature for the programmers. Also, the SSH support helps to manage the servers quickly.
Linux Distributions
Many agencies modified the Linux operating system and makes their Linux distributions. There are
many Linux distributions available in the market. It provides a different flavor of the Linux operating
system to the users. We can choose any distribution according to our needs. Some popular distros are
Ubuntu, Fedora, Debian, Linux Mint, Arch Linux, and many more.
For the beginners, Ubuntu and Linux Mint are considered useful and, for the proficient developer,
Debian and Fedora would be a good choice. To Get a list of distributions, visit Linux Distributions
.
Linux System Programming, Instructor: Tante Henry S. Page 5 of 38
How does Linux work?
Linux is a UNIX-like
operating system, but it supports a range of hardware devices from phones to supercomputers. Every Linux-based
operating system has the Linux kernel and set of software packages to manage hardware resources.
Also, Linux OS includes some core GNU tools to provide a way to manage the kernel resources, install
software, configure the security setting and performance, and many more. All these tools are packaged
together to make a functional operating system.
Advantages of Linux
Linux is an open-source operating system like Windows and MacOS. It is not just limited to the
operating system, but nowadays, it is also used as a platform to run desktops, servers, and embedded
systems. It provides various distributions and variations as it is open source and has a modular design.
The kernel is a core part of the Linux system.
Linux system is used to manage various services such as process scheduling, application scheduling,
basic peripheral devices, file system, and more. Linux provides various advantages over other
operating systems such as Windows and macOS. So, it is used in almost every field, from cars to home
appliances and smartphones to servers (supercomputers).
In this section, we will see some major advantages of the Linux system. Further, we will see the
advantages of Linux over other operating systems and will determine why it is better than other
operating systems.
op 20 Advantages of Linux
Linux System Programming, Instructor: Tante Henry S. Page 6 of 38
Following are top 20 advantages of the Linux operating system:
1. pen Source
As it is open-source, its source code is easily available. Anyone having programming knowledge can
customize the operating system. One can contribute, modify, distribute, and enhance the code for any
purpose.
2. Security
The Linux security feature is the main reason that it is the most favorable option for developers. It is not
completely safe, but it is less vulnerable than others. Each application needs to authorize by the admin
user. The virus is not executed until the administrator provides the access password. Linux systems do
not require any antivirus program.
3. Free
Certainly, the biggest advantage of the Linux system is that it is free to use. We can easily download it,
and there is no need to buy the license for it. It is distributed under GNU GPL (General Public License).
Comparatively, we have to pay a huge amount for the license of the other operating systems.
4. Lightweight
5. Stability
Linux is more stable than other operating systems. Linux does not require to reboot the system to
maintain performance levels. It rarely hangs up or slow down. It has big up-times.
6. Performance
Linux system provides high performance over different networks. It is capable of handling a large
number of users simultaneously.
7. Flexibility
Linux operating system is very flexible. It can be used for desktop applications, embedded systems, and
server applications too. It also provides various restriction options for specific computers. We can install
only necessary components for a system.
8. Software Updates
In Linux, the software updates are in user control. We can select the required updates. There a large
number of system updates are available. These updates are much faster than other operating systems.
So, the system updates can be installed easily without facing any issue.
9. Distributions/ Distros
There are many Linux distributions available in the market. It provides various options and flavors of
Linux to the users. We can choose any distros according to our needs. Some popular distros
are Ubuntu, Fedora, Debian, Linux Mint, Arch Linux, and many more.
For the beginners, Ubuntu and Linux Mint would be useful and, Debian and Fedora would be good
choices for proficient programmers.
The programmers prefer the Linux terminal over the Windows command line. The package manager on
Linux system helps programmers to understand how things are done. Bash scripting is also a functional
feature for the programmers. It also provides support for SSH, which helps in managing the servers
quickly.
14. Privacy
Linux always takes care of user privacy as it never takes much private data from the user. Comparatively,
other operating systems ask for the user's private data.
15. Networking
Linux facilitates with powerful support for networking. The client-server systems can be easily set to a
Linux system. It provides various command-line tools such as ssh, ip, mail, telnet, and more for
connectivity with the other systems and servers. Tasks such as network backup are much faster than
others.
16. Compatibility
Linux is compatible with a large number of file formats as it supports almost all file formats.
17. Installation
Linux installation process takes less time than other operating systems such as Windows. Further, its
installation process is much easy as it requires less user input. It does not require much more system
configuration even it can be easily installed on old machines having less configuration.
Linux History
Evolution of Computer
In earlier days, computers were as big as houses or parks. So you can imagine how difficult it was to
operate them. Moreover, every computer has a different operating system which made it completely
worse to operate on them. Every software was designed for a specific purpose and was unable to
operate on other computer. It was extremely costly and normal people neither can afford it nor can
understand it.
Evolution of Unix
In 1969, a team of developers of Bell Labs started a project to make a common software for all the
computers and named it as 'Unix'. It was simple and elegant, used 'C' language instead of assembly
language and its code was recyclable. As it was recyclable, a part of its code now commonly called
'kernel' was used to develop the operating system and other functions and could be used on different
systems. Also its source code was open source.
Initially, Unix was only found in large organizations like government, university, or larger financial
corporations with mainframes and minicomputers (PC is a microcomputer).
Unix Expansion
In eighties, many organizations like IBM, HP and dozen other companies started creating their own
Unix. It result in a mess of Unix dialects. Then in 1983, Richard Stallman developed GNU project with
the goal to make it freely available Unix like operating system and to be used by everyone. But his
project failed in gaining popularity. Many other Unix like operating system came into existence but
none of them was able to gain popularity.
Evolution of Linux
He started it just for fun but ended up with such a large project. Firstly he wanted to name it as 'Freax'
but later it became 'Linux'.
He published the Linux kernel under his own license and was restricted to use as commercially. Linux
uses most of its tools from GNU software and are under GNU copyright. In 1992, he released the kernel
under GNU General Public License.
Linux Today
Today, supercomputers, smart phones, desktop, web servers, tablet, laptops and home appliances like
washing machines, DVD players, routers, modems, cars, refrigerators, etc use Linux OS.
Linux Features
o Multiuser capability: Multiple users can access the same system resources like memory, hard
disk, etc. But they have to use different terminals to operate.
o Multitasking: More than one function can be performed simultaneously by dividing the CPU
time intelligently.
o Portability: Portability doesn't mean it is smaller in file size or can be carried in pen drives or
memory cards. It means that it support different types of hardware.
o Security: It provides security in three ways namely authenticating (by assigning password and
login ID), authorization (by assigning permission to read, write and execute) and encryption
(converts file into an unreadable format).
o Live CD/USB: Almost all Linux distros provide live CD/USB so that users can run/try it without
installing it.
o Graphical User Interface (X Window system): Linux is command line based OS but it can be
converted to GUI based by installing packages.
o Support's customized keyboard: As it is used worldwide, hence supports different languages
keyboards.
o Application support: It has its own software repository from where users can download and
install many applications.
o File System: Provides hierarchical file system in which files and directories are arranged.
o It is an open source OS which gives a great advantage to the programmers as they can design
their own custom operating systems.
o It gives you a lot of option of programs having some different features so you can choose
according to your need.
o A global development community look at different ways to enhance its security, hence it is highly
secured and robust so you don't need an anti virus to scan it regularly. Companies like Google,
Amazon and Facebook use linux in order to protect their servers as it is highly reliable and stable.
o Above all you don't have to pay for software and server licensing to install Linux, its absolutely
free and you can install it on as many computers as you want.
o Its completely trouble free operating system and don't have an issue with viruses, malware and
slowing down your computer.
Linux Bash
The Linux Bash is also known as 'Bourne-again Shell.' It is a command language interpreter for the
Linux based system. It is a replacement of Bourne shell (sh). It was developed under the GNU Project
and written by Brian Fox. Nowadays, Bash is the default user shell of most of the Linux distributions.
The Linux/Unix shell allows us to interact with the Linux system through the commands. It let us invoke
an executable file to create a running process. Moreover, it also allows us to interact with the Linux file
system. It is designed in such a way that we can perform all the Linux operations through Bash.
It offers various functional improvements over Bourne Shell (sh) for both interactive and programming
use. Although many sh scripts can be run by Bash without any change. The Bash contains the following
improvements over sh:
It can be downloaded from the official GNU server via both HTTP (http://ftp.gnu.org/gnu/bash/) and
FTP (ftp://ftp.gnu.org/gnu/bash/ ) servers.
People often get confused between bash, shell, and shell script. Let's walk through shell and scripting
to clear a picture of bash, shell, and scripting.
What is Shell
If we are a new Linux user, and we open the terminal, it is assumed that we are well confused as to what
to do with it. Here the Shell comes in the role.
The terminal contains the shell; it allows us to execute the commands to interact with the system. We
can perform various operations such as store and retrieve data, process information, and various other
simple as well as complex tasks.
To open the terminal, press CTRL+ALT+T keys. Perform some basic operations such as date, cal,
ls, and pwd to take a tour with it.
What is Scripting
Suppose we are required to execute some basic commands every day, for example above four
commands. Linux supports a feature called scripting that allows us to execute more than one task at
once. So, it is good to define a script rather than performing repetitive tasks.
To understand how to use Linux script, let's define a script in a combination of some tasks. To define a
script, create a file with a .sh extension. We are using the VI text editor. However, any text editor can be
used to define a script. Consider below command:
1. vi tasks.sh
The above command will open the vi editor in normal mode. Switch it to insert mode by pressing ESC,
and after that enter 'i' keys, enter your desired tasks. Every task should be defined in a new line.
Consider the below tasks:
1. date
2. cal
3. pwd
4. ls
After entering the tasks, press ESC and :wq! Keys to save and exit from the editor.
1. chmod +x tasks.sh
We have created our first script. To execute the shell script, execute the file name by predefining './
' with the file name. Consider the below command:
1. ./task.sh
As we can see from the above output, by using the shell, we can automate the tasks. We can use this
whenever we need it. It can be used repeatedly any number of times.
Features of Bash
All the built-in command of the sh shell is available in Bash; moreover, it facilitates us with many other
features. Some key features of Bash are as follows:
o Shell Syntax: The shell syntax contains shell operations, quoting, and comments. The shell
operations are the basic operation of the shell. Quoting allows how to remove the special
meaning from characters, and comments are meant to specify the comments.
o Shell commands: Shell commands are the types of commands that you can execute. These
commands can be simple commands, pipelines, lists, compound commands, and more.
o Shell Functions: Shell functions are used to group commands by name. They are executed as
traditional commands. When we use the name of a shell function, the list of commands
associated with that is executed.
Linux System Programming, Instructor: Tante Henry S. Page 15 of 38
o Shell parameters: Basically, a parameter is an entity that stores value; it can be a name, number,
or special character. The shell parameters specify how the shell store value. They can be a
positional parameter or a special parameter. Positional parameters are the shell's command-line
arguments, and the special parameters are denoted by a special character.
o Shell Expansions: Shell expansion is a technique that is used by Bash to expand the parameters.
Expansion is performed on the command line after the input has been splitted into tokens.
o Redirections: It is a way to manage and control the input and output.
o Command execution: It decides how the system will react when we execute a command.
o Shell Scripts: It is a text file that has shell commands and executes them when it is used. Bash
reads and executes the commands then exits.
Unix Vs Linux
Today Linux is in great demand. You can see the use of Linux everywhere. It's dominating on our servers,
desktop, smartphones and even used in some electrical devices like refrigerators.
Some people think Unix and Linux as synonyms, but that's not true. Many operating systems were
developed to be like Unix but none of them got the popularity as Linux. Linux is the clone of Unix. It
has several features similar to Unix, still have some key differences. Before Linux and Windows,
computer world was dominated by Unix. Unix is a copyrighted name and IBM AIX, HP-UX and Sun
Solaris are only Unix operating system remained till date.
Examples It has different distros like Ubuntu, Redhat, Fedora, IBM AIX, HP-UX and Sun Solaris.
etc
Users Nowadays, Linux is in great demand. Anyone can It was developed mainly for servers,
use Linux whether a home user, developer or a workstations and mainframes.
student.
Development As it is open source, it is developed by sharing and Unix was developed by AT&T Labs,
collaboration of codes by world-wide developers. various commercial vendors and
non-profit organizations.
Manufacturer Linux kernel is developed by the community of Unix has three distributions IBM
developers from different parts of the world. AIX, HP-UX and Sun Solaris. Apple
Although the father of Linux, Linus Torvalds also uses Unix to make OSX
oversees things. operating system.
GUI Linux is command based but some distros provide Initially it was command based OS,
GUI based Linux. Gnome and KDE are mostly used but later Common Desktop
GUI. Environment was created. Most
Unix distributions use Gnome.
Interface The default interface is BASH (Bourne Again SHell). It originally used Bourne shell. But is
But some distros have developed their own also compatible with other GUIs.
interfaces.
File system Linux supports more file system than Unix. It also supports file system but
support lesser than Linux.
Coding Linux is a Unix clone,behaves like Unix but doesn't Unix contain a completely different
contain its code. coding developed by AT&T Labs.
Security It provides higher security. Linux has about 60-100 Unix is also highly secured. It has
viruses listed till date. about 85-120 viruses listed till date
In this section, we will discuss the differences between Linux and Windows by considering several
parameters such as performance, usability, security, ease of use, and more to clear a picture of
using both operating systems. Further, we will see the advantages of Linux over other operating
systems such as Windows and Mac OS. This will help you to decide which one is better for you.
Microsoft Windows is a family of various operating systems. It comes with two versions, i.e., 64 bit and
32 bit. It facilitates both client and server versions. The latest client version is Windows 10, and the server
version is Windows server 2019.
Windows is a straight forward and simple to use. Generally, it is designed for users having no
programming knowledge. So, mostly it is used for business and alternative industrial purposes.
Linux is a collection of operating systems that are based on Linux kernel. The first version of Linux was
released in the year 1991. The Linux system is most commonly used for servers; however, it is available
in desktop versions as well.
Ubuntu, Devian, and Fedora are some popular Linux distributions. Also, we have SUSE Linux
Enterprise Server (SLES) and RedHat Enterprise Linux for the commercial distribution of Linux. As it
is open-source, we can modify the source code and make variations in the operating system.
Let's discuss some features and parameters to understand the difference between both operating
systems:
o File System
But Linux uses a tree structure to store and organize files. Linux file structure starts from the root
directory, and it is considered as a start point of the file system. It is represented by a forward slash (/).
In Linux, everything (Directories, devices, and files) is considered a file.
o General files
o Directory files
o Device files
Directory files: Directory files are the depository for other files. A directory can have a subdirectory file
within it. For Windows, we can understand them as folders.
Device files: Windows represents the external devices (Pendrives, hard drives, and CD-ROM) as letters
such as E: F: But, Linux represents devices as files, such as the hard drive's partitions are represented
as dev/sda1, dev/sda2, and more (depends upon the number of partitions). All the device files take
place within the directory /dev.
Linux files are case sensitive; therefore, we can have two files having the same name; one in upper case
and other in lower case. Comparatively, the Windows files are not case sensitive; we cannot have two
files with the same name.
o Users
o Administrator
o Standard
o Child
o Guest
o Regular
o Administrative(root)
o Service
Regular User
In Linux, when we install ubuntu on our system, a regular account user is created. By default, all our files
are saved in the home directory (/home/). A regular user cannot access the other user's directories.
Root User
Apart from the regular user account, a root user account is also created during installation. The root
account is also called as superuser as it can access the restricted files, install the software and other
utility, and has administrative rights. For installing the software or editing the system files or any other
Linux System Programming, Instructor: Tante Henry S. Page 21 of 38
administrative task, we need the root access. For general tasks such as creating files, playing games,
browsing the internet, we don't need the root access.
Service user
The Linux system is widely used as the server operating system. The leading service providers such as
Apache, Squid, email, and more have their service accounts to increase security. Linux service user can
allow or deny access to various resources depending on the service type.
Note: The Ubuntu Desktop version does not have a service account, and the regular accounts are
called standard accounts.
o Home Directory
Linux OS has a separate home directory for a user. The files and directories created by a user are stored
under a particular home directory. A user cannot store the files under any other user's directory, as it is
not allowed to access the other user's directory. For example, the home directory of a user 'Alex' is
automatically created as "/home/alex/" at the time of installation.
The Windows OS also has different home directories for different users. It is as "C:\ documents or
\settings."
o Other Directories
Linux systems have a tree structure to save the directories; comparatively, Windows uses different drives
to save the directories. In Windows, system and program files usually take place in C drive. But in Linux,
the system and program files take place in their specific directory such as software files are stored in
/bin, programs and device files are in /dev, and boot files are stored in /boot directory.
o Kernel
The basic part of any operating system is its kernel. It interacts with hardware devices and performs
other functions such as process management, file handling, and more. Different operating systems have
different kernels.
Linux and Windows have a different kernel. The Linux kernel is monolithic, and it consumes more
running space. Comparatively, Windows uses the microkernel, which consumes less running spaces.
But, Windows running efficiency is less than Linux. The key difference between Linux kernel and
Windows kernel is that Windows is a commercial software while Linux is open-source software.
o Pricing
Linux is an open-source operating system, so almost all the utilities and libraries are entirely free. The
GNU/Linux distribution can be simply downloaded for free. However, some companies are providing
paid support for their Linux distributions, but the underlying software is still free.
Linux System Programming, Instructor: Tante Henry S. Page 22 of 38
Microsoft Windows licensed copy usually cost between $99.00 and $199.00. Microsoft currently stops
providing support for the earlier versions. The latest version, Windows 10, is available at $139.
Let's focus some head to head comparison between Linux and Windows.
Access Users can access the source code of Usually, users cannot access the source
kernel in Linux and can alter the kernel code. However, members of some groups
according to need. can have access to it.
Variety Linux has several distributions that are Windows have fewer options to customize.
highly customizable.
Command- The command line usually referred Windows also have a command line, but it is
line to as Terminal, which is the most not such effective as a comparison to the
useful tool of the Linux system. It used Linux terminal. Most users prefer the GUI
for administration and daily tasks. For options for daily tasks.
the end-users, it does not look so
effective.
Installation The Linux installation process is a bit Windows OS is easy to install and set up on
complicated to set up as it requires a machine; it requires fewer user input
many user inputs. It takes less time options during installation. However, it
than Windows to install. takes more time to install as compared to
Linux.
Ease of use The Linux OS is meant to be for the Windows comes with simple and rich GUI
technical user because you must have options, so it is easy to use it. It can be
some exposure to various Linux simply used by technical as well as non-
commands. Users may take more time technical users. The troubleshooting
to be a handy user of Linux. The process is also much easy than Linux.
troubleshooting process is also
complicated as compared to Windows.
Reliability Linux is highly reliable and secure. It Windows is not as much reliable as Linux.
has well-established system security, However, now Windows has improved
process management, and uptime. reliability but still has some security
weaknesses and system instabilities.
Update Linux provides full control to its users Windows updates are annoying. The
on updates. A user can install the updates will come at any time and take too
update whenever needed. Also, it much time to install. Sometimes, you power
takes less time to install an update. on your machine, and updates are
automatically getting started.
Unfortunately, the user does not have much
control over updates.
Security Linux OS is more secure than Windows is less secure than Linux. Attackers
Windows. It is hard for the hackers and primarily target the Windows for malware
attackers to find a loophole in it. So, and virus. Windows is most vulnerable
Linux is hard to breakthrough. without anti-virus.
But Linux is different from them. Different parts of Linux are developed by different organizations.
Different parts include kernel, shell utilities, X server, system environment, graphical programs, etc. If
you want you can access the codes of all these parts and assemble them yourself. But its not an easy
task seeking a lot of time and all the parts has to be assembled correctly in order to work properly.
From here on distribution (also called as distros) comes into the picture. They assemble all these parts
for us and give us a compiled operating system of Linux to install and use.
Earlier, Ubuntu uses GNOME2 desktop environment but now it has developed its own unity desktop
environment. It releases every six months and currently working to expand to run on tablets and
smartphones.
2) Linux Mint
Mint is based on Ubuntu and uses its repository software so some packages are common in both.
Earlier it was an alternative of Ubuntu because media codecs and proprietary software are included in
mint but was absent in Ubuntu. But now it has its own popularity and it uses cinnamon and mate
desktop instead of Ubuntu's unity desktop environment.
3) Debian
Debian has its existence since 1993 and releases its versions much slowly then Ubuntu and mint.
Ubuntu is based on Debian and was founded to improve the core bits of Debian more quickly and make
it more user friendly. Every release name of Debian is based on the name of the movie Toy Story.
Red hat uses trademark law to prevent their software from being redistributed. CentOS is a community
project that uses red hat enterprise Linux code but removes all its trademark and make it freely available.
In other words, it is a free version of RHEL and provide a stable platform for a long time.
5) Fedora
It is a project that mainly focuses on free software and provides latest version of software. It doesn't
make its own desktop environment but used 'upstream' software. By default it has GNOME3 desktop
environment. It is less stable but provides the latest stuff.
Linux mint It works like windows and should be use by new comers.
CentOS If you want to use red hat but without its trademark.
OpenSUSE It works same as Fedora but slightly older and more stable.
Arch Linux It is not for the beginners because every package has to be installed by yourself.
Linux Licensing
Linus Torvalds has given linux kernel license to GNU General Public License (GPL) version 2. GNU make
sure that any software source code licensed under it have to make originating source code open and
freely availble to all its users. Here, freely doesn't mean by cost but it means that it is freely available to
users to distribute and modify the code.
There is the third version of GNU, GNU Lesser General Public License (LGPL) version 3. But it imposes
some more permissions on the license. Torvalds doesn't like some provisions in version 3 and have
announced that linux kernel will not come under version 3.
o PATH
This variable contains a list of directories in which our system looks for files. It separates directories by
a (:) colon.
o USER
o HOME
This variable holds the default path to the user's home directory.
o EDITOR
o UID
o TERM
o SHELL
This variable contains the path to the default shell that is being used by the user.
o ENV
To create a new variable, use the export command followed by a variable name and its value.
Syntax:
1. export NAME=VALUE
1. export new_variable=10
1. echo new_variable
To display the value of the variable, use the $ symbol before the variable name:
1. echo $new_variable
1. export PATH=$PATH:/home/jdk1.8/bin/
We can also create a user to define a variable by directly declaring it on the terminal.
1. new_variable2=100
1. env
Output:
SHELL=/bin/bash
SESSION_MANAGER=local/javatpoint-GB-BXBT-2807:@/tmp/.ICE-unix/1458,unix/javatpoint-GB-
BXBT-2807:/tmp/.ICE-unix/1458
COLORTERM=truecolor
XDG_CONFIG_DIRS=/etc/xdg/xdg-ubuntu:/etc/xdg
XDG_MENU_PREFIX=gnome-
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GTK_IM_MODULE=ibus
LANGUAGE=en_IN:en
QT4_IM_MODULE=ibus
MANDATORY_PATH=/usr/share/gconf/ubuntu.mandatory.path
GNOME_SHELL_SESSION_MODE=ubuntu
SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
XMODIFIERS=@im=ibus
DESKTOP_SESSION=ubuntu
SSH_AGENT_PID=1362
GTK_MODULES=gail:atk-bridge
PWD=/home/javatpoint
LOGNAME=javatpoint
XDG_SESSION_DESKTOP=ubuntu
XDG_SESSION_TYPE=x11
GPG_AGENT_INFO=/run/user/1000/gnupg/S.gpg-agent:0:1
XAUTHORITY=/run/user/1000/gdm/Xauthority
GJS_DEBUG_TOPICS=JS ERROR;JS LOG
WINDOWPATH=2
HOME=/home/javatpoint
USERNAME=javatpoint
IM_CONFIG_PHASE=1
LANG=en_IN
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01
:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=0
1;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:
*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=0
1;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.
bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01
;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*
.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=0
1;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01
;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:
Linux System Programming, Instructor: Tante Henry S. Page 29 of 38
*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mp
eg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=
01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:
*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=0
1;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*
.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.og
g=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
XDG_CURRENT_DESKTOP=ubuntu:GNOME
VTE_VERSION=5802
GNOME_TERMINAL_SCREEN=/org/gnome/Terminal/screen/dc6e48ed_7868_43d5_a086_fd6d6a90a74a
INVOCATION_ID=fadf556d1cf5422ea55b247bf57c3c99
MANAGERPID=1252
CLUTTER_IM_MODULE=ibus
GJS_DEBUG_OUTPUT=stderr
LESSCLOSE=/usr/bin/lesspipe %s %s
XDG_SESSION_CLASS=user
TERM=xterm-256color
DEFAULTS_PATH=/usr/share/gconf/ubuntu.default.path
LESSOPEN=| /usr/bin/lesspipe %s
USER=javatpoint
GNOME_TERMINAL_SERVICE=:1.216
DISPLAY=:0
SHLVL=1
QT_IM_MODULE=ibus
XDG_RUNTIME_DIR=/run/user/1000
JOURNAL_STREAM=9:35179
XDG_DATA_DIRS=/usr/share/ubuntu:/usr/local/share/:/usr/share/:/var/lib/snapd/desktop
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/g
ames:/snap/bin
GDMSESSION=ubuntu
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
_=/usr/bin/env
javatpoint@javatpoint-GB-BXBT-2807:~$
To remove an environment variable, execute the unset command followed by variable name:
The above command will delete the specified variable and its components from the system.
To remove a variable new_variable from the system, execute the command as follows:
1. unset new_variable
Syntax:
1. set [options]
To know about how to set the environment variable in Linux, visit Here.
o -a: It is used to mark variables that are modified or created for export.
o -b: It is used to notify of job termination immediately.
o -e: It is used to exit immediately if a command exits with a non-zero status.
o -f: It is used to disable the file name generation (globbing).
o -h: It is used to save the location of commands where they looked up.
o -k: It is used to place all assignment arguments in the environment variable of a command, except those
that precede the command name.
o -m: It is used to enable Job control.
o -n: It is used to read commands.
o -o: It is used for option-name.
o -p: It is used to disable the processing of the '$ENV' file and import shell functions. It is turned on
whenever the real and effective user ids do not match. Turning off this option may cause the working uid
and gid to be set as the authorized uid and gid.
o -t: It is used to exit from the command after executing one command.
o -u: It is used to treat unset variables as an error when substituting.
o -v: It is used to print shell input lines.
o -x: It is used to print commands and their arguments in a sequential way (as they are executed).
o -B: It is used to perform brace expansion by the Shell.
o -C: It is used to disallow existing regular files to be overwritten by redirection of output.
o -E: It is used if the ERR trap is inherited by the shell functions.
Linux System Programming, Instructor: Tante Henry S. Page 31 of 38
o -H: It is used to enable style history substitution. By default, it is on when the shell is interactive.
o -P: It is used if we do not want to follow symbolic links when executing commands.
o -T: If this flag is set, the DEBUG trap is inherited by the shell functions.
Note: To turn off the above flags, use the '+' rather than '-' symbol.
To better understand the set command, let's see a brief introduction of the shell variable:
Shell Variable
A variable is a character string that can hold a value. The assigned value could be anything such as
number, filename, text, or any other data type. It is just like a pointer to the actual data. The shell allows
us to create, delete, and assign variables.
The variable name can have any letters, numbers, or underscore (_) character. We cannot have the
characters such as "! *, or -", because these special characters have other meanings for the shell.
According to Unix naming convention, the Unix shell variable must have their names in UPPERCASE.
The '-x' option is used with the set command to show command and their arguments. It is useful for
debugging the shell script.
1. set -x
1. set +x
1. set -C
Getting Help
If you get stuck during the use of the set command, you can access the help documentation from your
terminal. To access the help manual, execute the below command:
1. set --help
Environment variables are set when we open a new shell session. At any time, if we change any variable
value, the shell has no way to select that change. The export command allows us to update the current
session about the changes that have been made to the exported variable. We do not need to wait to
start a new shell session.
Syntax:
The basic export command will display all the exported environment variables of your system. It is
executed as follows:
1. export
To display all the exported environment variable of the current shell, execute the command with -p
option as follows:
1. export -p
To use a function with the export command, use the -f option. If we do not use this option, it will be
considered as a variable, not function.
Syntax:
1. export -f function_name
1. export -f name
1. name
1. function hello
2. > {
3. > echo hello, welcome to javatpoint
4. > }
1. export -f hello
The export command allows us to assign a value before exporting a function. Consider the below
command:
1. export name[=value]
1. a=5
1. export a
Linux System Programming, Instructor: Tante Henry S. Page 36 of 38
we can verify the assignment by using the printenv command as follows:
1. printenv a
The vim editor is the most widely used text editor for the Linux systems. We can set the vim as default
text editor by using the export command.
To set the vim as a default text editor, execute the following command:
1. export EDITOR=/usr/bin/vim
2. export | grep EDITOR
The above commands will not show any confirmation. Consider the below output:
To create a new variable, use the export command followed by a variable name and its value.
Syntax:
1. export NAME=VALUE
1. export sys=50
1. echo sys
To display the value of the variable, use the $ symbol before the variable name