Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Ultimate Linux Shell Scripting Guide

You're reading from   The Ultimate Linux Shell Scripting Guide Automate, Optimize, and Empower tasks with Linux Shell Scripting

Arrow left icon
Product type Paperback
Published in Oct 2024
Publisher Packt
ISBN-13 9781835463574
Length 696 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Donald A. Tevault Donald A. Tevault
Author Profile Icon Donald A. Tevault
Donald A. Tevault
Arrow right icon
View More author details
Toc

Table of Contents (26) Chapters Close

Preface 1. Getting Started with the Shell 2. Interpreting Commands FREE CHAPTER 3. Understanding Variables and Pipelines 4. Understanding Input/Output Redirection 5. Customizing the Environment 6. Text-Stream Filters – Part 1 7. Text Stream Filters – Part 2 8. Basic Shell Script Construction 9. Filtering Text with grep, sed, and Regular Expressions 10. Understanding Functions 11. Performing Mathematical Operations 12. Automating Scripts with here Documents and expect 13. Scripting with ImageMagick 14. Using awk – Part 1 15. Using awk – Part 2 16. Creating User Interfaces with yad, dialog, and xdialog 17. Using Shell Script Options with getops 18. Shell Scripting for Security Professionals 19. Shell Script Portability 20. Shell Script Security 21. Debugging Shell Scripts 22. Introduction to Z Shell Scripting 23. Using PowerShell on Linux 24. Other Books You May Enjoy
25. Index

Understanding Shell Sessions

Any time you initiate interaction with a shell, you’re creating a shell session. Shell sessions can be classified in the following ways:

  • Interactive shells: When you sit down at a computer and enter commands on the command-line, you’re working with an interactive shell.
  • Non-interactive shells: When a shell session is invoked from within a shell script, you’re working with a non-interactive shell.
  • Login shells: If you log into a Linux machine that’s running in text mode, without a graphical interface, you’re working with a login shell. You can also work with a login shell on a desktop machine by invoking a Ctrl-Alt-Function_Key sequence to switch away from the desktop interface to a text mode terminal. (You can use function keys F1 through F6 for this.) Or, you can invoke the bash -l command in the normal terminal emulator to open a child bash session in login mode. The final way to initiate a login shell session is to log into a machine remotely via Secure Shell. Regardless of whether the remote machine is of a text-mode or GUI-mode variety, your remote session will be of the login shell type.
  • Non-login shells: Any time you open a terminal emulator on a desktop Linux machine, you’re working with a non-login shell.

So, what does all this mean? Well, the difference between interactive and non-interactive shells is fairly obvious, so I won’t talk more about that. But, I would like to point out two different ways to know whether you’re working with a login shell or a non-login shell.

The first way is to use the shopt command, like so:

[donnie@fedora ~]$ shopt login_shell
login_shell    	off
[donnie@fedora ~]$

The shopt command can be used to set various configuration options for a bash session. Here though, I’m using it without any option switches to just view the login_shell setting. You see here that the login_shell setting is off, which means that I’m in a non-login shell here on my Fedora workstation. On my text mode Fedora Server virtual machine, the shopt output looks like this:

[donnie@fedora-server ~]$ shopt login_shell
login_shell    	on
[donnie@fedora-server ~]$

As you see, the login_shell parameter is on.

The second way to tell if you’re in a login shell is to use the echo $0 command, like this:

[donnie@fedora ~]$ echo $0
bash
[donnie@fedora ~]$

The $0 argument is what’s known as a positional parameter. I’ll provide in-depth coverage of positional parameters in Chapter 8, Basic Shell Script Construction, so don’t stress out about them just yet. All you need to know for now is that the echo $0 command shows the name of the script or executable that’s currently in use.

In this case, we’re in a bash session, which means that the bash executable is in use. But, how do we know whether or not we’re using a login shell? Well, it’s just that the bash output is not preceded by a dash, which means that we’re not in a login shell. To show the difference, here’s what you’ll see on the text-mode Fedora Server virtual machine:

[donnie@fedora-server ~]$ echo $0
-bash
[donnie@fedora-server ~]$

The -bash output indicates that I’m in a login shell.

Even from afar, I can read your mind. (Yes, I know that that’s creepy.) I know that you’re wondering why you need to know about these different types of shell sessions. Well, it’s just that there are several different bash configuration files. The type of shell session you’re using determines which configuration files the session accesses. So, now that you know about the different types of shell sessions, we can look at these configuration files.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image