Linux Programming
Jean de Dieu MWUNGURA
Linux Programming
1
Linux Shell Commands
2
Linux (OS) Architecture
3
What is Shell
• The shell is a program that takes
commands from the keyboard and gives
them to the operating system to
perform.
• Is interface between user and the
kernel(OS).
• When user types in the terminal,
shell interprets the command and calls
the program that user wants.
• There are various kind of commands
4
like C Shell, Bourne Shell and Korn Shell
shell command Syntax
• Syntax:
command [option] [argument]
– eg: ls -l
• Note: some commands dons't have any
option or don't accept any argument
such as 'clear' and 'pwd'.
• 'clear' command clears out all the
previous commands and outputs from
terminal display.
• The 'pwd' command stands for 'print
Main category of shell Commands
• Built-in commands:
• are internal commands that are built-
in the shell. Eg: 'pwd', 'help', 'type',
'set', 'unset', source, cd, fg ,etc.
• External commands:
• are other than built-in commands and
are located in the filesystem.
Examples: ls, cat etc.
6
Built-in commands:
– Are internal commands that are built-in
the shell.
– are called from the shell and executed
directly within the shell itself.
– You can list all built-in commands with
the help of 'help' or 'compgen -b'
command.
– execution of them is fast. (in the sense that the
shell doesn’t have to search the given path for them in the
PATH variable, and also no process needs to be spawned for
executing it)
7
External commands:
– are other than built-in commands.
– Are located in the filesystem.
– These are totally shell independent.
– execution of them is not fast in the
sense that the shell doesn’t have
them, it need to search them firtst
– Mostly these commands reside in
/bin, /sbin, /usr/sbin.
– Examples: ls, cat etc.
8
How to check if command is
internal or external?
How to find out whether a command is
internal or external? type command
Syntax: type specific command eg:
9
Different types of command
Different types of command are :
• Linux Aliases
• Linux Shell Built-in
• Program Executables (File System
Commands):
• Linux Shell Functions:
• Linux Shell Reserved
Words(Keyword)
10
list down all the available commands Linux?
Syntax: compgen –flag
eg: compgen –k to print all keywords command
– In the above syntax, the flag (c,a,k,b,A) is a placeholder
that can be replaced according to our needs.
Syntax
-c − to list all the commands you could run.
-a − to list all the aliases.
-k − to list all the keywords.
-b − to list all the built-ins.
-A - function − used to list all the functions you could run.
11
Linux 'alias' command
What is Linux Aliases command?
– Are user-defined commands, they are created using the
alias shell built-in command.
– alias is a shell built-in command.
Why Linux Aliases command?
– It is used to converts a complicated command into a
simpler command by creating a shortcut to replace the
simple one.
How it work?
– Making 'alias' in command line creates a temporary
'alias'. Temporary aliases are only available until you exit
the shell. To make permanent 'alias' store it in bash
startup files.
12
Implementation of Linux 'alias'
command
• To list all aliases on your system uses
compgen –a
• Note: There will be no space on either
side of (=) sign while typing 'alias'
command. Quotes are necessary if there
are more than one word(parametter) in
the string being aliased.
13
Creation of Linux 'alias'
command
Descript Syntax Example
ion
Alias for alias newName=command alias nyereka=ls
one
comman
d
Alias for alias newName='command arg1 alias show=’nyereka -l’
more arg2....'
than one
paramet
er
Alias for alias newName='/home/sssit/ alias gerahome='/
path: path/...' home/sssit/.. '
14
Removing alias
How to remove alias
With the help of 'unalias' command
you can remove created alias.
Syntax:
unalias <createdAlias>
eg: unalis show
15
Linux Shell Built-in
Commands
• Linux Shell Built-in Commands are
Linux commands built into the shell.
Eg:pwd, cd, bg, alias, history, type, s
ource, read, exit, and many others.
• You can list or check Linux built-in
commands using compgen –b
16
File System Commands
• File system commands are instructions
that allow users to interact with the file
system of an operating system through a
command-line interface (CLI). These
commands help manage files and
directories, perform various operations
such as creating, moving, copying, and
deleting files, and navigate the file
system structure.
• Eg: cat · cd · cp · ls · mkdir · mv · popd ·
17
Linux File System Commands
• ls - List directory contents.
• cd - Change the current directory.
• pwd - Print the current working directory.
• mkdir - Create a new directory.
• rmdir - Remove a directory.
• rm - Remove files or directories.
• cp - Copy files or directories.
• mv - Move or rename files or directories.
• touch - Create an empty file or update the timestamp of an
existing file.
• cat - Concatenate and display file content.
• chmod - Change file permissions.
• chown - Change file owner and group.
• ln - Create links between files.
18
Shell Reserved Words(keyword):
• Keyword are the words with specialized
meaning to the shell
• Eg:
if, then, fi, for, while, case, esac, else, un
til, and many others are shell-reserved
words.
• all keywords command printed by:
compgen –k
19
Linux Shell Functions
• Shell functions are a way to group commands
for later execution (reusability )using a single
name for the group
• Promote code readability and maintainability.
• Shell functions can take parameters, execute
commands, and return values.
• They are similar to functions in traditional
programming languages but are used within the
context of shell scripting.
20
Defining shell function
• Syntax
21
Thank you