4/27/24, 2:16 AM Shell Programming - DEV Community
Oluchi John
Posted on Apr 26
18 2 1 4 3
Shell Programming
#shell #linux #bash #gitbash
In today's rapidly evolving tech landscape, mastering the command line streamlines the
development process for developers. The command line can be accessed through
various shell environments like Bash and PowerShell. This article explores the shell
environment, showing its role in executing complex operations, navigating files,
processing data, and text manipulation.
Definition of shell
A shell is a user interface that allows users to interact with a computer's operating
system using text-based commands. Think of it as a translator between the user and the
operating system
The shell takes the human-readable commands entered by the user and translates them
into instructions that the operating system understands and executes, it also displays
the results or output of these commands back to the user. This type of interaction,
called a command-line interface (CLI), gives you more direct control over the system
https://dev.to/ollie20/shell-programming-4k65?context=digest 1/11
4/27/24, 2:16 AM Shell Programming - DEV Community
compared to graphical user interfaces (GUIs).
With Command Line Interface, you can run programs, manage files, and perform various
tasks.
Importance of Shell
Shell allows users to interact with the operating system. This makes it easier to
execute complex tasks by typing specific commands.
It allows users to automate repetitive tasks using written shell scripts.
It helps administrators and users access remote systems through protocols like SSH
It can be used to access and manage files and directories.
It diagnoses issues and checks system status.
Shell Environments
There are several shells available, each with its own unique features and syntax. Some of
the most widely used shells include:
Bash (Bourne-Again Shell)
PowerShell
Zsh (Z Shell)
Bash (Bourne-Again Shell): This is one of the shells on Linux distributions and macOS.
Its syntax is compatible with the original Unix shell, making it a popular choice.
PowerShell: PowerShell is a shell environment pre-installed on Windows systems by
default and it is object-oriented.
Zsh (Z Shell): Zsh is known for its extensive customization options and advanced
features. It offers improved tab completion, history management, and plugin support.
This article uses the bash shell for code examples and commands. If you're unfamiliar
with Bash, consider using Git Bash for Windows or the built-in Terminal app on macOS.
Both offer a Bash-like experience that you can follow along with in this article.
Shell Navigation and operations
Navigating the file system through the command line is one of the foundational skills in
mastering the shell environment. The ability to change directories, manage files, and
understand pathnames is essential for effective command-line operations. A directory is
https://dev.to/ollie20/shell-programming-4k65?context=digest 2/11
4/27/24, 2:16 AM Shell Programming - DEV Community
known as a folder in the Windows system.
The various commands for navigating the file system are :
Commands Function
This command is used to change your current working directory.
cd(Change
You can change to a different directory by typing cd followed by
Directory)
the directory name or path. eg: cd newfile.
pwd(Print
This command shows the path of the current directory you're
Working
working with.
Directory)
ls This command is used to list the contents of a directory.
Command syntax
Some commands have components that modify their function. These components are
called options and arguments.
Options are used to adjust the operation of the command while arguments are what the
command operates on. Eg:
ls -l myfile
-l is the option while myfile is the argument.
Here is a table that contains command options and their function:
Commands Function
ls -a List all contents including hidden ones
ls -t List all file contents by the time they were last modified
cd - This will take you to the previous directory
cd .. This will take you to the previous directory
cd ~username This will take you to the directory of the specified username
rm -r This will delete directories and their contents.
https://dev.to/ollie20/shell-programming-4k65?context=digest 3/11
4/27/24, 2:16 AM Shell Programming - DEV Community
Commands Function
ls -l list all file contents in a long format
File and directory manipulation
mkdir: This command is used to create directories, eg: mkdir newDirectory
touch: This command creates an empty file eg: touch Mynewfile creates a file named
"Mynewfile"
mv: This command can both move files/directories and rename them, eg: mv oldfile
newfile ,this moves or renames oldfile to newfile.
cp: This command is used to copy files and directories, eg: cp Myfile backup copies
"Myfile" to a "backup" directory.
rm: This command deletes files, eg: rm unwanted.txt
rmdir: This command deletes empty directories, eg: rmdir emptydir
File Handling and Text Processing
Working with Files and Directories:
There are text editors that allow you to create, view, and modify text files directly from
the command line. Some of these popular text editors include:
Nano
Vim
Emacs
Viewing File Content
cat: The cat command displays the entire contents of a file in the terminal.
less: The less command allows you to view the contents of a file one screen at a time,
enabling navigation and searching.
head and tail: These commands display the first or last few lines of a file,
respectively.
Text Processing with grep, sed, and awk
grep: The grep command is a powerful tool for searching for specific patterns or text
within files. It can be combined with other commands for more advanced operations.
https://dev.to/ollie20/shell-programming-4k65?context=digest 4/11
4/27/24, 2:16 AM Shell Programming - DEV Community
Example: grep "keyword" myFile
sed: The sed command performs text transformations on an input stream. It's
particularly useful for finding and replacing text within files.
awk: The awk command processes and analyzes text line by line. It's often used for
data extraction and reporting.
Remote System Administration with Shell
Shell access to remote systems is achieved through protocols like Secure Shell (SSH).
SSH is a private key that enables you to access a remote server; it ensures a secure
connection by encrypting the communication between the user's machine and the
remote server.
Brief Overview Of How It Works
To establish an SSH connection, administrators provide the remote server's address and
authentication credentials such as a username and password or a key pair
ssh your_username@remote-machine-address
Replace "your_username" with your actual username, and "remote-machine-address"
with the provided address.
If you're using key-based authentication, it could look like this:
ssh -i /path/to/your/private/key your_username@remote-machine-address
Don't worry if you don't see characters appearing as you type; it's a security feature.
Once it is done, this allows administrators to execute commands, manage files, and
perform various administrative tasks on the remote machine.
File Permissions and Ownership
Operating systems offer access rights to files and directories in the shell environment.
This determines what actions can be taken on the files and directories. This allows
remote workers to access files from a different system. You can use ls -l command to
view the permission settings of a file.
There are three basic permissions types :
https://dev.to/ollie20/shell-programming-4k65?context=digest 5/11
4/27/24, 2:16 AM Shell Programming - DEV Community
Read(r): This allows a user to view a directory or file content.
Write(w): This allows a user to modify or delete a file, as well as create or remove
files within a directory.
Execute(x): This grants permission for file execution.
File permissions are set for three categories of users:
Owner: The user who created the file or directory.
Group: A designated group of users.
Others: Anyone who does not fall into the owner or group categories.
- rwx rwx rwx
| | |
owner group users
This means the owner, group, and others can read, write, and execute the file. The -
indicates it is a file, d indicates a directory.
rw-rw-rw- means the owner, group, and others can only read and write the file.
File permissions can also be represented
numerically:
Numeric Representation is a three-digit number where each digit corresponds to a
permission type and user category.
-rwx rwx r--
| | |
111 111 100
111 in binary = 7, 100 in binary = 4
Therefore -rwx rwx r-- is represented numerically as 774.
Changing Permissions
The chmod command is used to modify file permissions. You can change permissions
either numerically or symbolically.
Numerical example: chmod 774 myfile
Symbolic example: chmod u+rwx myfile
Changing Ownership (chown)
https://dev.to/ollie20/shell-programming-4k65?context=digest 6/11
4/27/24, 2:16 AM Shell Programming - DEV Community
The chown command is used to change the owner and group of a file or directory.
chown username filename
This assigns ownership of filename to the user named username
chown username:groupname filename
This assigns ownership of filename to user named username and sets the group
ownership to groupname
chown -R username:groupname directory
This recursively changes the ownership of all files and subdirectories within directory to
username and groupname .
Input and Output Redirection
Redirection is an essential technique for manipulating the input and output of a
command.
Redirection allows you to redirect where the output of a command goes and where a
command gets its input.
Output redirection: The > symbol is used to redirect the output of a command to a
file. If the command doesn't exist, If the file doesn't exist, it's created; if it exists, it's
overwritten.
Append redirection: The >> symbol is similar to the output redirection but instead
of overwriting the file, it adds the output to the end of an existing file.
Input redirection
Pipes (|)
A pipe | is used to send the output of one command as the input to another. This is
used to connect commands.
Eg: ls -l | grep "file"
In this example, the ls -l command lists files in detailed format, and the output is then
filtered by grep to display only lines containing the word "file."
Pipes and redirection can be combined to perform complex operations.
Eg: ls -l | grep "file" | sort > sorted_file_list.txt
https://dev.to/ollie20/shell-programming-4k65?context=digest 7/11
4/27/24, 2:16 AM Shell Programming - DEV Community
In the next article, we will look at advanced shell techniques and shell scripting.
👋 Before you go
Do your career a favor. Join DEV. (The website you're on right now)
It takes one minute and it's fee.
Get started
Top comments (3)
GrimKillingbeck • Apr 26
This was a great overview of Shell commands. You refreshed a few things for
me, especially since I've just creating my own scripts!
Anita Olsen 💜 • Apr 26
What an absolutely brilliant article! ✨ Saved!
Ini • Apr 26
Awesome piece. Such an eye opener!
Code of Conduct • Report abuse
Pieces.app PROMOTED
A Workflow Copilot. Tailored to You.
https://dev.to/ollie20/shell-programming-4k65?context=digest 8/11
4/27/24, 2:16 AM Shell Programming - DEV Community
Our desktop app, with its intelligent copilot, streamlines coding by generating
snippets, extracting code from screenshots, and accelerating problem-solving.
Read the docs
Oluchi John
I'm a software engineer, also interested in AI and robotics.
JOINED
May 31, 2023
Trending on DEV Community
Javascript factorial
https://dev.to/ollie20/shell-programming-4k65?context=digest 9/11
4/27/24, 2:16 AM Shell Programming - DEV Community
#discuss #webdev #javascript #programming
What's the Best Question to Ask in a Meeting?
#discuss
How Are You Celebrating Earth Day? 🌱💧
#discuss
AWS PROMOTED
https://dev.to/ollie20/shell-programming-4k65?context=digest 10/11
4/27/24, 2:16 AM Shell Programming - DEV Community
Expert security advice without the buzzwords
AWS and AWS Partners tackle common security questions
Get expert advice around all things security as AWS and AWS Partners go in depth and discuss a new
security topic each episode—streaming live every other Wednesday at 8AM PT.
Learn More
https://dev.to/ollie20/shell-programming-4k65?context=digest 11/11