Customizing the Environment
local graph
Chapter 5 — Customizing the Environment
Section titled “Chapter 5 — Customizing the Environment”In this chapter, we’ll look at the various configuration files for the bash shell environment. We’ll look at how to customize these configuration files, and how to set certain environmental options from the command-line.
Topics in this chapter include:
- Reviewing the environmental variables
- Understanding the configuration files
- Setting shell options from the command line
I’m sticking with bash for now, but in Chapter 22, Understanding the Z Shell, I’ll explain how zsh is set up.
If you’re raring and ready, let’s get going.
Technical Requirements
For this chapter, you’ll need one Fedora virtual machine and one Debian virtual machine. I won’t be providing a Hands-On Lab in this chapter. Instead, I’ll just invite you to follow along on your virtual machines as you read through the chapter.
Reviewing the Environmental Variables
In Chapter 3, Understanding Variables and Pipelines, I introduced the concept of environmental variables. In this chapter, I’d like to expand on that topic.
We’ve already seen that environmental variables can be used to help customize and control your shell environment. Here’s a table of some of the more common environmental variables:
| Environmental Variable | Purpose |
|---|---|
| USER | The username of the person who is currently logged into the system. |
| UID | The User ID number of the logged-in user. |
| EUID | The Effective User ID number of the user who’s running a certain process. |
|---|---|
| This defines the path to the mail spool of the logged in user. | |
| SHELL | The path to the shell that is currently in use. |
| PWD | The current working directory. (PWD stands for “Print Working Directory”). |
| OLDPWD | The previous working directory. |
| HOSTNAME | The hostname of the computer. |
| PATH | A colon-delimited list of directories in which the system looks when you type an executable program name. This variable gets built in several configuration files, such as /etc/profile and the .bashrc file that resides in the user’s home directory. |
| HOME | This holds the path for the current user’s home directory. Some programs will use this variable to determine where to find configuration files or determine the default location to store files. |
| PS1 | The primary shell prompt. |
| PS2 | The secondary shell prompt. |
| TERM | This specifies the current terminal type. You’ll probably see it set as either xterm or some form of xterm for non-login sessions and login sessions from a remote GUI terminal emulator, and just Linux for login sessions at the local console. (I’ll explain the types of sessions in the next section.) The system needs to know which terminal type is in use so that it will know how to move the cursor and display text effects in text-mode programs. |
| DISPLAY | This variable allows you to have multiple displays running from the same computer. If you’re only running one display, you’ll see a returned value of :0. (That means, the first display on the current computer.) |
| EDITOR | This sets the default text editor that you want to use for system administrative functions such as systemctl edit and crontab -e. Your best choices are generally either either nano or vim. |
Table 5.1: The more common environmental variables
The slightly tricky part about this is that environmental variables aren’t always exactly the same across all Linux distros. For example, the EDITOR variable is defined here on my Fedora workstation, but it’s not used at all on any Debian or Ubuntu distros. Instead, Debian and Ubuntu use another mechanism for setting the default editor, which we’ll look at in a bit.
Now, before I can fully explain the bash configuration files, I need to explain the different types of shell sessions.
Understanding Shell Sessions
Section titled “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 -1 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_shelllogin_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_shelllogin_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 $0bash[donnie@fedora ~]$The 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.
Understanding the Configuration Files
As we’ve already seen, you can set environmental variables from the command-line. But, any variables you set in this manner will only last for the duration of the command-line session. When you log out of the system or close the terminal emulator window, any environmental changes that you’ve made from the command-line will be lost.
What if we want to make these changes permanent? There are several configuration files that we can edit to save our changes. Some are global and will affect all users, while others will only affect an individual user. The slightly tricky part is that the files differ from one Linux distro to the next. Let’s start by looking at the bash configuration files on Fedora. After that, we’ll move on to Debian.
bash Global Configuration Files on Fedora
As we’ve already seen, there are global configuration files in the /etc/ directory, and users’ configuration files in each user’s own home directory. For the global configuration, we have these two files:
- /etc/profile
- /etc/bashrc
The /etc/profile file sets up the environment for anyone who opens a bash login shell session, and is executed as soon as a user logs in. Go ahead and open it and look through it. What you’ll see is a somewhat complex shell script that performs the following functions:
- It defines the default PATH setting for both the root user and every non-root user who’s using a login shell.
73
- It defines the various environmental variables that are associated with each logged-in user. These include the UID, EUID, USER, LOGNAME, and MAIL variables.
- It sets the machine’s HOSTNAME.
- It sets the HISTSIZE variable, which defines how many past commands will be held in each user’s command history.
- After having done all of the above, it reads in the various configuration scripts in the /etc/profile.d/ directory. Most of these scripts define system-wide behaviors for certain system utilities. There’s also a script that sets the default EDITOR for system utilities that automatically open a text editor.
Of course, at this point I don’t expect you to completely understand what’s going on in this profile file. However, what you see in this file is the kind of stuff that we’ll be covering throughout the remainder of this book. Once you’ve gone through all of that, you’ll be able to come back and have a better understanding of exactly what this file is doing.
The other global configuration file is the /etc/bashrc file, which affects non-login shell sessions. It does a few different things for us, but for our present purposes just know that this is where the PS1 variable is defined for interactive sessions, and that it defines the PATH setting for users who are using a non-login shell.
In addition to these two main files, there are also supplementary configuration files in the /etc/profile.d/ directory, as you see here:
[donnie@fedora profile.d]$ lsbash_completion.sh colorzgrep.sh gawk.csh sh.localcolorgrep.csh colorzgrep.csh gawk.sh vim-default-editor.cshcolorgrep.sh colorzgrep.sh lang.csh vim-default-editor.shcolorls.csh csh.local lang.sh which2.cshcolorls.sh debuginfod.csh less.csh which2.shcolorxzgrep.csh debuginfod.sh less.sh[donnie@fedora profile.d]$Each of these files contains supplementary configuration information for the shell environment. In general, each file does either one of these two things for us:
- Creating aliases for certain other commands. (We’ll cover aliases in just a bit.)
- Defining environmental variables that aren’t already defined in the /etc/profile or /etc/bashrc files.
For a simple example of the second function, let’s look at the definition for the EDITOR variable. On the server version of Fedora it will be in the /etc/profile.d/vim-default-editor.sh file, and on the workstation version of Fedora it will be in the /etc/profile.d/nano-default-editor.sh file. Here’s what the server version looks like:
# Ensure vim is set as EDITOR if it isn't already set
if [ -z "$EDITOR" ]; thenexport EDITOR="/usr/bin/vim"fiAgain, I don’t expect you to fully understand what’s going on with this just yet. So for now, the simple explanation is that if the EDITOR environmental variable hasn’t yet been set, then it will be set it to /usr/bin/vim. (On the workstation edition of Fedora, EDITOR will be set to /usr/bin/nano.)
There may be times when an administrator needs to alter the default environmental settings that have been defined in either /etc/profile or /etc/bashrc. You could do that by editing the /etc/profile or the /etc/bashrc file, but that’s not recommended. Instead, just place the new settings in the /etc/profile.d/sh.local file. Currently, there’s nothing in that file except for an explanatory comment, as we see here:
#Add any required envvar overrides to this file, it is sourced from /etc/profileOkay, that about does it for the global configuration files on Fedora. Let’s now look at the users’ own configuration files.
Users’ Configuration Files on Fedora
The shell configuration files in the users’ home directories are considered as hidden files, because their filenames begin with a dot. To see them, you’ll need to use the -a option with ls, like so:
[donnie@fedora ~]$ ls -latotal 20drwx------. 3 donnie donnie 111 Aug 26 16:28 .drwxr-xr-x. 3 root root 20 Aug 19 18:15 ..-rw-------. 1 donnie donnie 1926 Aug 26 15:51 .bash_history-rw-r--r--. 1 donnie donnie 18 Feb 5 2023 .bash_logout-rw-r--r--. 1 donnie donnie 141 Feb 5 2023 .bash_profile-rw-r--r--. 1 donnie donnie 492 Feb 5 2023 .bashrc-rw-------. 1 donnie donnie 37 Aug 26 16:28 .lesshstdrwx------. 2 donnie donnie 48 Aug 21 14:13 .ssh[donnie@fedora ~]$Only three of these files are of concern to us, which are:
- .bash_logout: This file is currently empty, except for an explanatory comment. Any commands that you place in this file will be executed either upon logging out of an interactive login shell session, or when the exit function is invoked at the end of a shell script. Among other things, you could use this to automatically clean up temporary files or to perform an automatic backup of files in a user’s home directory when the user exits the shell session.
- .bash_profile: This file is only used for login shell sessions. So, if you open a terminal emulator on your desktop machine, anything that’s in this file will have no effect. If you look at the contents of this file, you’ll see that the only thing it does by default is to cause the .bashrc file to be read in.
- .bashrc: This is the main bash configuration file for the user level, which directly affects non-login shell sessions. Since the .bash_profile file causes the .bashrc file to be read in for login shell sessions, anything you place into .bashrc affects both login and non-login sessions.
So, how does this work in practice? Well, let’s say that you need to add the /opt/ directory to your working PATH. Currently, the PATH looks like this:
[donnie@fedora-server ~]$ echo $PATH/home/donnie/.local/bin:/home/donnie/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin[donnie@fedora-server ~]$To add the /opt/ directory, open the .bashrc file in your text editor, and look for this stanza:
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]then PATH="$HOME/.local/bin:$HOME/bin:$PATH"fiChange it so that it looks like this:
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]then PATH="$HOME/.local/bin:$HOME/bin:$PATH:/opt:"fiLog out and then log back in. Your PATH setting should now look like this:
[donnie@fedora-server ~]$ echo $PATH/home/donnie/.local/bin:/home/donnie/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt:[donnie@fedora-server ~]$You can now place executable scripts or binary executable files in the /opt/ directory, and run them without having to specify the entire path.
To make a permanent change to your command prompt setting so that it will also show the current date and time, place this line at the end of the .bashrc file:
export PS1="[\\d \\t \\u@\\h \\w] \\$ "Log out and log back in. Your command prompt should now look like this:
[Tue Aug 29 17:13:55 donnie@fedora-server ~] $So, that’s all pretty cool, right? For even more coolness, let’s move on to the Debian configuration files.
bash Global Configuration Files on Debian
On Debian, things are somewhat different. The /etc/profile file is still there, but it’s radically different from the one on Fedora. Go ahead and look at it on your own virtual machine. You’ll see that all it does is define the PATH and PS1 variables and read in any supplementary files that are in the /etc/profile.d/ directory. If the session is an actual bash session instead of an sh session, then it will also read in the /etc/bash.bashrc file. Also, you’ll see that Debian uses a method for defining PATH that’s radically different from the method that’s used on Fedora.
Instead of the /etc/bashrc file that Fedora uses, Debian uses the /etc/bash.bashrc file. This file does the following things for us:
- It checks the size of the window in which the bash session is running, so that the correct number of lines and columns can be displayed after the user enters a command.
- It defines the PS1 variable.
- If a user enters a command that bash can’t find, it invokes the command_not_found_handle function if the command-not-found software package isn’t installed.
At the top of the bash.bashrc file, you’ll see that it’s invoked for interactive shell sessions, and that it will also get invoked for login shell sessions if the profile file sources it. We’ve already seen that the profile file does source it, so we know that bash.bashrc does run for both login and non-login interactive sessions.
When a configuration file reads in information from another configuration file, we say that the first file is sourcing the second file.
Unlike on Fedora, the /etc/profile.d/ directory doesn’t do much for us by default. The only thing you’ll see there is the bash_completion.sh script. As you’ll see in a moment, the aliases that are defined in the /etc/profile.d/ directory on Fedora are defined in the users’ configuration files on Debian.
Users’ Configuration Files on Debian
In each user’s home directory on a Debian system, we have these bash configuration files:
- .bash_logout: This is the same as the .bash_logout file on Fedora, except that it contains a command that will clear the screen upon logout from a login session.
- .profile: This replaces the .bash_profile that’s on Fedora, and serves the same purpose.
- .bashrc: It does the same things as the .bashrc file on Fedora, and more besides. It also defines some of the environmental variables that are defined globally on Fedora, and it also defines some of the aliases that are defined in the /etc/profile.d/ directory on Fedora.
As I’ve already told you, I don’t expect you to read through any of these configuration files and completely understand what they’re doing. That’s why I’ve only shown you a few small snippets of these files instead of trying to explain each one in detail. After you’ve completely gone through this book, you’ll have gained a better understanding of them.
I should also point out that every family of Linux distros has its bash configuration files set up differently. I can’t cover all of them, but you can probably figure out what’s going on with them from what I’ve told you here.
Setting the Default Editor on Debian
Section titled “Setting the Default Editor on Debian”Oh, dear, I almost forgot to show you how to change the default editor for Debian, since we can’t do it by setting an environmental variable. So, let’s look at that now.
On a Debian system, the default editor is defined by a pair of symbolic links, as you see here:
donnie@debian:~$ ls -l /usr/bin/editorlrwxrwxrwx 1 root root 24 Jan 18 2023 /usr/bin/editor -> /etc/alternatives/editordonnie@debian:~$ cd /etc/alternatives/donnie@debian:/etc/alternatives$ ls -l editorlrwxrwxrwx 1 root root 9 Jan 18 2023 editor -> /bin/nanodonnie@debian:/etc/alternatives$The editor symbolic link in the /usr/bin/ directory points to the editor symbolic link in the /etc/alternatives/ directory, which in turn points to the nano executable that’s in the /bin/ directory. This tells us that nano is set as the default editor. To change the default editor on a Debian machine, you’ll first need to ensure that the editor that you want to use is installed. Then, use the update-alternatives utility, like so:
donnie@debian:~$ sudo update-alternatives --config editor[sudo] password for donnie:There are 3 choices for the alternative editor (providing /usr/bin/editor).Selection Path Priority Status--------------------------* 0 /bin/nano 40 auto mode1 /bin/nano 40 manual mode2 /usr/bin/vim.basic 30 manual mode3 /usr/bin/vim.tiny 15 manual modePress <enter> to keep the current choice[*], or type selection number: 2update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor(editor) in manual modedonnie@debian:~$I’ve chosen the number 2 option to set vim as the default editor. Let’s see how that changed the symbolic link in the /etc/alternatives/ directory:
donnie@debian:~$ ls -l /etc/alternatives/editorlrwxrwxrwx 1 root root 18 Sep 2 15:26 /etc/alternatives/editor -> /usr/bin/vim.basicdonnie@debian:~$Very cool. This allows me to use my favorite editor.
Okay, now that we’ve seen the configuration files, let’s look at another way to customize a shell session.
Setting Shell Options from the Command-line
In addition to using environmental variables to modify your shell session, you can also use shell options. You would use the set command to set shell options from either the command-line or from within your shell scripts. You can also use it from the command-line to just view the options that are set.
You can use either the Fedora or the Debian virtual machine for this section. With only one minor exception that concerns a man page, everything will be the same on both of them.
To begin, let’s invoke the set command without any options or arguments, like so:
[donnie@fedora-server ~]$ setBASH=/bin/bashBASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:globskipdots:histappend:interactive_comments:login_shell:patsub_replacement:progcomp:promptvars:sourcepath......quote_readline (){ local ret; _quote_readline_by_ref "$1" ret; printf %s "$ret"}[donnie@fedora-server ~]$What you’ll see is a complete list of environmental variables and shell functions that are active for this shell session. (I’ll tell you more about shell functions in Chapter 10, Understanding Functions.)
Use the set -o command to just see the list of shell options that are active, like this:
[donnie@fedora-server ~]$ set -oallexport offbraceexpand onemacs onerrexit offerrtrace offfunctrace offhashall onhistexpand onhistory on......xtrace off[donnie@fedora-server ~]$I won’t go over what each and every one of these options does, but I will tell you how to find out what they do. The only catch is that there’s a bit of a trick to it. That is, there’s no man page that’s specifically for the set command. When you do man set on your Fedora machine, you’ll actually pull up the bash man page. On Debian, you’ll have to do man bash, because man set won’t work at all. The reason for this is that the set command is built into the bash executable, and doesn’t have its own executable. Once you have the bash man page up, you’ll need to search through it to find the explanation of the set command and all of its options. For now, let’s look at a couple of the more useful options and how to set them.
In Chapter 4, Understanding Input/Output Redirection, I told you how the noclobber option can help prevent you from accidentally overwriting an important file. By default, noclobber is off, as you see here:
[donnie@fedora-server ~]$ set -o | grep noclobbernoclobber off[donnie@fedora-server ~]$The way you turn options on and off is completely counterintuitive. You’ll use the set -o command to turn an option on and the set +o command to turn an option off, which is the exact opposite of what you’d think it would be. (I have no idea why someone decided to do things this way. But, whatever, right?) So, let’s say that we want to turn the noclobber option on. Here’s what that looks like:
[donnie@fedora-server ~]$ set -o noclobber[donnie@fedora-server ~]$ set -o | grep noclobbernoclobber on[donnie@fedora-server ~]$Now, let’s turn it back off:
[donnie@fedora-server ~]$ set +o noclobber[donnie@fedora-server ~]$ set -o | grep noclobber<!-- page: 094 (printed 80) -->noclobber off[donnie@fedora-server ~]$For some options, such as noclobber, you can use a shorthand notation. So, to minimize keystrokes while turning noclobber on or off just do this:
[donnie@fedora-server ~]$ set -C[donnie@fedora-server ~]$ set -o | grep noclobbernoclobber on[donnie@fedora-server ~]$ set +C[donnie@fedora-server ~]$ set -o | grep noclobbernoclobber off[donnie@fedora-server ~]$In this example, the -C replaces -o noclobber, and +C replaces +o noclobber.
Another option that you might find useful is the allexport option. To see how you might use it, let’s create a programming variable from the command-line, and then try to use it in a child shell. Here’s t that looks like:
[donnie@fedora-server ~]$ car="1958 Edsel Corsair"[donnie@fedora-server ~]$ echo $car1958 Edsel Corsair[donnie@fedora-server ~]$ bash[donnie@fedora-server ~]$ echo $car[donnie@fedora-server ~]$ exitexit[donnie@fedora-server ~]$As you see, once I opened a child shell with the bash command, the value of car is no longer available to me. One way to fix that is to precede the variable definition with export, like so:
[donnie@fedora-server ~]$ export car="1958 Edsel Corsair"[donnie@fedora-server ~]$ bash[donnie@fedora-server ~]$ echo $car1958 Edsel Corsair[donnie@fedora-server ~]$ exitexit[donnie@fedora-server ~]$This time, the value of car is available to me in the child shell. The problem with this method is that I would have to remember to precede each variable definition I create with export. It would be easier just to set the allexport option so that every variable I create will be exported automatically. Here’s how that works:
[donnie@fedora-server ~]$ set -o allexport[donnie@fedora-server ~]$ set -o | grep allexportallexporton[donnie@fedora-server ~]$ car="1964 Ford Galaxie"[donnie@fedora-server ~]$ bash[donnie@fedora-server ~]$ echo $car1964 Ford Galaxie[donnie@fedora-server ~]$ exitexit[donnie@fedora-server ~]$This time, the car variable got exported automatically so that its value is available in the child shell. When you’re done with allexport, you can turn it off, like so:
[donnie@fedora-server ~]$ set +o allexport[donnie@fedora-server ~]$ set -o | grep allexportallexportoff[donnie@fedora-server ~]$The -a shorthand notation is also available for allexport, which looks like this:
[donnie@fedora-server ~]$ set -a[donnie@fedora-server ~]$ set -o | grep allexportallexporton[donnie@fedora-server ~]$ set +a[donnie@fedora-server ~]$ set -o | grep allexportallexportoff[donnie@fedora-server ~]$And now, my shell option settings are back to their default values.
Keep in mind that any options that you set will return to normal when you exit from the shell session.
Okay, that does it for shell options, at least for now. In Chapter 21, Debugging Shell Scripts, I’ll show you a couple of more cool tricks that you can do with them. With all this out of the way, let’s move on to the final section of this chapter.
Understanding Aliases
If you ever watch any crime dramas on television, you might see some criminal who uses more than one name. Of course, only one of those names is the criminal’s real name. All of the other names are fake names, or aliases, that the criminal uses to prevent being found by the police. In the world of operating system shells, aliases are quite useful and have nothing to do with criminal activity. In fact, you’re already using them without realizing it.
Think of an alias as a command that you can use in place of another command. For example, let’s say that you’re one of those poor souls who’s stuck using Windows most of the time, and who only occasionally gets the chance to work with Linux.
Now, let’s say that every time you get on a Linux machine, you instinctively always type in Windows commands, as you’re doing here:
[donnie@fedora-server ~]$ ls[donnie@fedora-server ~]$ ls -a. .. .bash_history .bash_logout .bash_profile .bashrc .lesshst .ssh.viminfo[donnie@fedora-server ~]$ cls-bash: cls: command not found[donnie@fedora-server ~]$Yeah, that cls command that you always use on Windows doesn’t work so well on Linux, does it? There’s an easy fix to that, though. Just create an alias that points to the clear command, like so:
[donnie@fedora-server ~]$ alias cls=clear[donnie@fedora-server ~]$Once you do this, you’ll be able to clear the screen with either the cls command or the clear command. Note that in this case, I didn’t have to surround the alias definition with quotes because it doesn’t contain any blank spaces or special symbols. Creating an alias that has blank spaces or special characters in its definition would look something like this:
Figure 5.1: Using my new lla alias on Fedora
Coolness, it works.
To see the list of all active aliases, just use the alias command without any options or arguments, like this:
[donnie@fedora-server ~]$ aliasalias cls='clear'alias egrep='egrep --color=auto'alias fgrep='fgrep --color=auto'alias grep='grep --color=auto'alias l.='ls -d .* --color=auto'alias ll='ls -l --color=auto'alias lla='ls -la --color=auto'alias ls='ls --color=auto'alias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias--read-functions --show-tilde --show-dot'......alias zgrep='zgrep --color=auto'[donnie@fedora-server ~]$As you see, they’re listed in alphabetical order. On Fedora, several aliases have already been globally-defined in a set of scripts in the /etc/profile.d/ directory. For an example, let’s look in the /etc/ profile.d/colorls.sh script. Most of the script is rather complex, consisting of commands that define the color scheme that will be used with the ls aliases. At the bottom, you’ll find the actual aliases, which look like this:
alias ll='ls -l --color=auto' 2>/dev/nullalias l.='ls -d .* --color=auto' 2>/dev/nullalias ls='ls --color=auto' 2>/dev/nullHere, we see that:
- The ll alias replaces the ls -l —color=auto command
- The l. alias replaces the ls -d .* —color=auto command
- The ls alias replaces the ls —color=auto command.
To see how these aliases work, create a few files and directories in the home directory of your Fedora virtual machine, like this:
[donnie@fedora-server ~]$ touch somefile.sh anyfile.txt graphic1.png[donnie@fedora-server ~]$ mkdir somedir[donnie@fedora-server ~]$ chmod u+x somefile.sh[donnie@fedora-server ~]$When you do a plain ls command, you’ll see that the plain text file shows up in either black or white letters, depending upon whether you’re using a black or a white background for your terminal. The directory shows up in blue letters, the graphic file shows up in magenta letters, and the executable script shows up in green letters. The ll alias performs the same function as the ls -l command, again with color coding, as you see here:
Finally, the l. alias shows you the files and directories whose names begin with a dot, again with color coding. It looks something like this:
The Debian developers have done things differently. Instead of defining aliases at the global level, the Debian folk just define them at the user level. You’ll find the list of defined aliases in the .bashrc file that’s in every user’s home directory. Only one is enabled by default, but you can easily enable any or all of the others by removing the # symbol from in front of them.
On the Fedora machine, there’s a which alias that replaces the which command. This alias shows you not only where the executable file for a command is, it also shows you if there’s an alias for that command. Here’s what that looks like:
[donnie@fedora-server ~]$ which lsalias ls='ls --color=auto'/usr/bin/ls[donnie@fedora-server ~]$There’s no which alias on Debian, so you can’t use which to see if there are any aliases for other commands. Here’s what the Debian output looks like:
donnie@debian:~$ which ls/usr/bin/lsdonnie@debian:~$It’s important to note that if you have an alias that has the same name as the actual underlying command, the alias always takes precedence. There are two ways to override that so that you can directly invoke the executable file for a command. The first way is to just specify the entire path to that executable, like this:
[donnie@fedora-server ~]$ /usr/bin/lsanyfile.txt somedir somefile.sh[donnie@fedora-server ~]$The second way is to precede the command with a backslash, like this:
[donnie@fedora-server ~]$ \lsanyfile.txt somedir somefile.sh[donnie@fedora-server ~]$You’ll now see the ls output without any color-coding. Let’s try it again with which, like this:
[donnie@fedora-server ~]$ /usr/bin/which ls/usr/bin/ls[donnie@fedora-server ~]$This time, which only shows the location of the executable file, and ignores the alias. If you need to disable an alias, use the unalias command, like this:
[donnie@fedora-server ~]$ which whichalias which='(alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot'/usr/bin/which[donnie@fedora-server ~]$ unalias which[donnie@fedora-server ~]$ which which/usr/bin/which[donnie@fedora-server ~]$In this case, the alias is only disabled temporarily, until you close the shell session. If you use unalias for any aliases that you’ve created from the command-line, that alias will be disabled permanently.
The last thing I’ll say about aliases is that any alias you set at the command-line will disappear when you exit the shell session. To make them permanent on either Debian or Fedora, just place them in the .bashrc file that’s in your home directory.
All right, that pretty much wraps things up for this chapter. Let’s summarize and move on.
Summary
In this chapter, we’ve completed laying the groundwork for the chapters to come. We explained the different types of shell sessions, and then looked at the configuration files that affect the shell environment at both the global and user levels. Then, we looked at how to set shell options and how to reate and use aliases.
Now that you know the basics of how a shell operates, you’re ready to tackle some meatier issues, like how to use text-stream filters. We’ll start with that in the next chapter. I’ll see you there.
Questions
-
Which two of the following commands turn on the noclobber option? (Choose two.)
a. set +o noclobber
b. set -o noclobber
c. set -C
d. set +C
-
On a Fedora machine, which of the following user-level configuration files will only affect a login shell session?
a. profile
b. .bash_profile
c. .bash.bashrc
-
You’ve just opened a terminal emulator on your desktop Linux machine. What type of shell session are you using? (Choose two.)
a. non-interactive
b. login
c. interactive
d. non-login
Further Reading
- How to List Environmental Variables on Linux: https://www.howtogeek.com/842780/linux-list-environment-variables/
- Types of Linux Shell Sessions: https://www.automationdojos.com/types-of-linux-shell-sessions/
- Linux set Command & How to Use it (9 examples): https://phoenixnap.com/kb/linux-set
Answers
- b and c
- c
- c and d
Join our community on Discord!
Read this book alongside other users, Linux experts, and the author himself.
Ask questions, provide solutions to other readers, chat with the author via Ask Me Anything sessions, and much more. Scan the QR code or visit the link to join the community.