Introduction to the Linux Terminal
Overview
Terminal in all Linux operating systems is a significant component that most users and developers prefer to use. It is because the Linux terminal is a robust tool that is shipped with all Linux operating systems. Users can accomplish a wide range of functionalities using it. We can execute different commands or run scripts within the terminal. Almost every task within the Linux operating system is possible using the terminal. This article will discuss what is linux terminal, a terminal emulator, running & combining popular commands in the terminal, writing scripts, etc.
An Introduction to the Linux Terminal
The Linux terminal is application software that runs on commands. This text-based app provides a command-line interface (CLI) to control & perform operations on Linux computers. The "terminal" is one of the most widely-used tools all Linux operating systems deliver as a default application. It helps Linux users accomplish almost all processes in the most effective way possible. When the successor of Linux, i.e., Unix, got developed, there was no GUI. All the operations, like opening a file, creating a file, etc., get performed using the command-line interface. We can install programming languages or write scripts using the Linux terminal. Since Linux terminal also works on servers and remote computers, server, and network administrators do not have to learn new ways separately to operate them.
What Is a Terminal Emulator?
Let us gather some insight into what is Linux terminal. Terminal emulators are programs that provide the ability of a computer terminal (usually of the PC) to run on a graphical environment. These programs help run terminals or CLIs like Linux terminals to run on top of the GUI environment so that users can access all the programs written originally to communicate with the other terminal kinds. To use a terminal emulator, users will install special programs on their computer or on a local area network (LAN) server with which it remains connected. Although most users use the graphical abilities of an operating system to perform their daily work, most Linux server users prefer using a terminal emulator to operate using the command-like interface.
Here is a list of well-known terminal emulators users prefer installing in their operating systems.
- Windows: Windows Terminal (CMD), PuTTy, ConEmu, etc.
- Mac OS X: Terminal (that comes by default), iTerm
- Linux: Gnome Terminal, Konsole, XTerm, etc.
Although all these terminal emulators have their features, these latest terminal emulators come with highlighted text features and tabbed windows.
Different Types of Linux Shells
All the Linux terminal remains connected to another program that interprets the code provided by the user. That program is the shell that makes the operating system understands what the user commands. The Linux shell is the outer layer of the operating system that interacts with the inner kernel. Two well-known options are there that the Linux terminal can leverage to perform its operations.
1. Bash (Bourne-Again shell): It is the most popular and the default Linux shell that comes pre-installed in most distributions. For default login purposes, Linux uses the bash shell. This shell is also available for other operating systems like Windows, Pop OS, Solaris 11, etc.
2. Zsh (Z shell): Z shell is another Linux shell that provides an environment to work as a command-line interpreter, login shell, and shell scripting. It runs on top of the bash shell & comes with additional features. All macOS and Kali Linux use the Z shell as their default shell. It offers flexibility by delivering extra features such as customization, theme support, plug-in support, spelling correction, etc.
Let us now closely look at the difference between Bash and Z shells.
- The bash shell is the default Linux shell, whereas the Z shell is not the default one. Users can set it to default manually.
- Bash utilizes the backslash symbols as escapes, whereas Z shell utilizes the percentage symbol.
- Bash will not support themes and plug-ins, whereas the Z shell does.
- Auto correction and text highlight features are not available in the bash shell. But the Z shell has it.
- The '.inputrc' and 'bind builtin' help in key binding in bash, whereas, in the Z shell, only the 'bind builtin' works.
Running Commands in the Linux Terminal
On operating systems like Linux, the terminal provides a text-based interface to type and run commands by determining the name of an executable file. Linux comes with a wide range of Linux commands & utilities that comes as a part of the operating system. These Linux commands allow us to install software packages, open a file, navigate the directories and file systems, configure systems, etc. The process is the instance of running a command in the terminal through the shell. We must note that almost everything (directory naming, commands, filenames, options, arguments, etc.) we utilize in Linux is case-sensitive. Let us take a closer look at various ways to run commands in the Linux terminal.
- Without Arguments or Options: When we run a command without any argument & option associated with that command, it comes under this category. Running commands in forms like cd, ls, IP, etc., are examples of Linux ccommandswithout arguments or options.
Example: $ ip
-
With arguments: Arguments are also known as parameters that are additional options that impact the behavior of the Linux command. These arguments or parameters make the command specific for the shell. Let's suppose we have the ls command that alone can display a list of all the files in a given directory. Now, if we specify the argument /, it will display all files and folders from the root directory. Example: $ ls /
-
With options: Numerous Linux commands take options or flags to modify the command's behavior. They get indicated or separated by a dash (-) followed by one or multiple flag texts. Some options also take da ouble dash (--) followed by the flag character(s). For example, the ls command has -l for long-listing format.
-
With option and arguments: Users can combine both arguments and options while running a Linux command. Let us consider a scenario where ls -la /home. Here, the command option is -la, which will flag out all files in long listing format, including the hidden ones that start with '.', and the argument is /home. The /home will list out the directories from the home directory.
Environment Variables
Some external variables directly interact with the various components of a Linux kernel or shell. They hold certain values as a universal entity to work with within the Linux environment.
What are Environment Variables?
Environment variables are dynamic named values that help determine and change how the commands & processes get executed. They help in configuring where and how various commands will run. These variables are fixed and allow users to customize how specific apps or services will behave within the system. Each variable comprises a name, which is usually in uppercase & an associated value (which is case-sensitive).
How to View All Environment Variables
- We can use the "env" command to view all environment variables. Here is a list of all environment variables that we use through the Linux operating system.
- BASH_VERSION: It contains the current bash instance's version.
- HOSTNAME: It holds the name of your computer.
- CDPATH: It helps in searching the path for the cd command.
- HISTFILE: It shows the filename in which command history remains saved.
- HISTSIZE: It determines the number of commands to remember from the command history.
- HOME: It holds the home directory of the current Linux user.
- IFS: This Internal Field Separator (IFS) determines the word splitting once expanded. By default, the value it takes is <space> <tab> <newline>.
- LANG: It determines the locale language category.
- PATH: The holds the search path for commands.
- PS1: It holds all the prompt settings.
- TMOUT: It determines the default time-out time for reading any built-in command.
- TERM: It holds the login terminal type.
- SHELL: It helps to determine the path to the login shell.
- DISPLAY: It is used to set X display name
- EDITOR: It helps users to set the default text editor's name.
View the Value of a Variable
We can use the echo command followed by the variable name within double quotes to view the current value of the variable. For example, echo "$HOME" It will display an output like: /home/your_username
Setting Environment Variables
To set or assign an environment variable with a specific value, we have to use the variable name followed by the the = (equal) operator and then the desired value. For example: `SHELL=/bin/she
Writing Scripts in the Linux Terminal
Linux shell scripting plays a significant role in automating various processes. We have to use the text editors like vi or vim to write scripts through the Linux terminal. Within the text editor, you can type the statements for execution. Here is an example:
We can also write scripts that might execute on startup, such as showing a particular message on establishing a new session or verifying specific environment variables.
Combining Multiple Commands in the Linux Terminal
-
Using semicolons (;): It is the most common way of running multiple commands in a terminal. The semicolon operator works amazingly. It will run all listed commands one after the other, irrespective of whether the previous Linux command ran properly or failed. Example: whoami ; hostname
-
Using OR (||) operator: The command following the OR operator, that is the second command will get executed if your system fails to implement the first command. It works like that of an else statement of any programming language. Example: The execution structure will look something like this: apt update || echo "failed"
-
Using AND (&&) operator: The AND operator will execute the subsequent command within that sequence only if the preceding Linux command gets executed successfully. Example: mkdir Download && cd Download
Popular Terminal-based Applications
-
Vim editor: The vim (Vi Improved) editor is one of the oldest text editors that help users to write scripts. It is the improvised version of the Vi editor. It features color schemes, automatic commands, tagging, digraph inputs (helpful in programming), split screens, etc. One can initiate Vim from the command line only.
-
GNU Emacs: It is another well-known text & code editor. Richard Stallman, the founder of the GNU project, developed it. With the Emacs text editor, one can draft an email, read a manual, write code, etc. It comes with incredible language support and extensions. It features cross-compatibility with different GNU apps and content-aware editing modes.
-
Gedit: Most Gnome desktop environments deliver this text editor known for its simplicity. This editor is an excellent tool for beginning in the Linux environment. This powerful tool supports the undo/redo options, multi-language spellchecking, syntax highlighting, and color schemes for different programming languages (Python, C++, Java, C, etc.).
Popular Linux Commands
pwd: It shows the working directory Linux commands.
- cd: Using this command, we can navigate through directories.
- ls: It helps in listing directories.
- mkdir: Using this command, we can create directories in Linux
- rm: It helps in deleting files or directories in the Linux systems.
- touch: It helps in creating blank files or empty files.
- ln: Using this command, we can create symbolic links.
- mv: It helps in moving or renaming files in Linux.
- cp: It works similarly to that of the mv but copies files.
- cat: It helps in displaying file contents on the terminal.
- clear: This command clears the terminal's display/screen.
- less: This command will show paged outputs through the terminal.
- echo: It will print any text that is written within double quotes following the command.
- man: Using this command, we can access manual pages for all commands.
- uname: This command helps us to grab some basic information about the operating system.
- whoami: It shows the active username.
- head: This command will return a particular number of lines from the top.
- tail: This command will return a particular number of lines from the bottom.
- tar: It extracts and compresses files with a .tar file extension.
- grep: It will look for a string within an output.
- diff: It will check the difference between two files.
- cmp: This command will determine whether two files are identical or not.
- ssh: It helps in giving Secure Shell command(s) in Linux.
- service: Using this command, one can start or stop various services.
- comm: It will combine the functionality of diff and cmp to check two files.
- sort: This will sort the file's content while outputting.
- export: It helps in exporting environment variables in Linux.
- ps: This command helps in displaying active processes.
- df: This command will display disk filesystem information.
- kill and kill all: This command will kill orterminatethat specific active process mentioned through process ID or name.
- mount: This command will mount file systems within your system.
- chmod: Using this command, we can change file permissions.
- chown: using this command, we can grant ownership of files or folders.
- user add and user mod: This command helps in adding new users or changing existing users' data.
- alias: It will create custom shortcut names for all frequently used commands.
- passwd: It helps in creating & updating passwords for the current user.
- ifconfig: This command displays network interfaces and IP addresses.
- traceroute: It will trace all the network hops from source to destination.
- whereas It helps to locate the source, binary, and manual pages for any command.
- whatis: It helps to identify what a command is used for.
- top: It will display all active processes that are life with their system usage.
Conclusion
- We hope this article has given a crisp idea of what is Linux terminal and why we need it.
- Then we discussed what terminal emulators are & why we need them, along with some examples.
- Next, we discuss the popular shells (bash and zsh) and their differences.
- The article also highlights the various ways of running a Linux command (with & without arguments or options).
- Then the article talks about environment variables, writing scripts through terminals, and how to combine multiple commands.
- In the end, we discussed some well-known terminal-based apps and text editors and some popular Linux commands.