The outermost layer of the operating system — the one you’re dealing with directly — is called the shell. Fedora comes pre-loaded with several different shells. The shell can be a graphical interface or a character interface. The two acronyms Graphical User Interface (GUI) and command-line Interface (CLI) are used in the documentation to distinguish between graphics and character-based shell/ interfaces. GNOME and Bash are Fedora’s default graphical and command-line interfaces, respectively, but you can also use other GUIs or CLI shells.

Next in this article, we’ll discuss some of the more recommended dot-file configurations for the Bash character interface.

An overview of the Bash

From the Bash Reference manual:

Basically, the shell is just a macro processor that executes commands. The term macro processor means extending text and symbols to form more complex expressions.

Bash Reference Manual 5th edition, Bash 5.0 May 2019

In addition to being able to use other programs, the Bash shell contains several built-in commands and keywords. Bash has so much built-in power that it can even stand alone as a high-level language. Several of Bash’s keywords and operators are similar to those in C.

Bash can be started in interactive or non-interactive mode. Bash’s interactive mode is a typical terminal/command line interface that many people are familiar with. The GNOME terminal opens Bash in interactive mode by default. An example of Bash running in non-interactive mode is when commands and data are piped to Bash from files or shell scripts. Other Bash modes include login, non-login, remote, POSIX, Unix SH, restricted, and using a different UID/GID mode than the user. Patterns can be combined with each other. For example, interactive + restricted + POSIX or non-interactive + non-login + remote. Different startup modes determine which startup files Bash reads. Understanding these modes of operation will help you modify your startup files.

According to the Bash Reference manual, it will:

  1. From the file… , as-cThe character of the argument passed to the call option… , or read input from the user’s terminal.
  2. Break the input into words and operators, following [its] reference rules. . These tags are separated by metacharacters. This step performs alias expansion.
  3. Parse tokens into simple and compound commands.
  4. Perform various shell expansions… To break the expanded tags into file names… And a list of commands and arguments.
  5. Perform the necessary redirection… And remove the redirection operator and its operands from the argument list.
  6. Execute a command.
  7. Wait for the command to complete if necessary and collect the exit status.

Bash Reference Documentation 5th edition, Bash version 5.0 May 2019

When the user enters the command line environment by starting the terminal emulator, an interactive shell session is started. GNOME terminals open the Shell for users in non-login mode by default. In Edit → Preferences → Profile → Command Edit → Preferences → Profilles → Command, you can configure the mode (logon and non-logon) in which the GNOME terminal starts. You can also ask Bash to enter login mode by passing it the -login flag when it starts. Note that Bash’s login mode and non-interactive mode are not mutually exclusive. You can have Bash run in both login mode and non-interactive mode.

Start the Bash

Unless the -noprofile option is passed in, the Bash shell in login mode reads and executes certain commands in the initialization file by default. If /etc/profile exists, it will be the first file to be executed, followed by the first file found in the order ~/.bash_profile, ~/.bash_login, or ~/.profile. When a user exits a shell in login mode, or when a script calls the built-in exit command in a shell in non-interactive login mode, Bash reads and executes the commands in ~/.bash_logout, if /etc/bash_logout exists, It will be executed immediately. In general, /etc/profile refers to the source /etc/bashrc file, reads and executes the commands in it, and then looks for and reads the.sh file in the /etc/profile.d directory that executes it. Similarly, ~/.bash_profile usually refers to the source ~/.bashrc file. Both /etc/bashrc and ~/. Bashrc are checked to avoid quoting the source repeatedly.

In Bash, the script is passed by source or. Command to import another script into it. This behavior is called “source” or “sourcing,” but there has never been a recognized and commonly used translation method for this behavior. After much deliberation, I think it can be translated as “quote” for the following reasons: 1. “quote” has the meaning of “quote, introduce”, which is consistent with the act; 2. 2. The pronunciation of the word “aid” is the same as the common Chinese meaning of “source”, so it is easy to remember. Above is our foolish view, for everyone reference discussion. — Lao Wang, 2020/7/19)

An interactive shell, if not a login shell, executes the ~/.bashrc file when it is first called. This is the type of shell that users typically enter when opening a terminal on Fedora. When Bash is started in non-interactive mode — just like when running a script — it looks for the BASH_ENV environment variable. If found, its value is expanded as the file name, and the file is read and executed. The effect is the same as executing the following command:

if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
Copy the code

It is worth noting that the value of the PATH environment variable is not used to search for the file name.

Important user point file

Bash’s best known user-point file is ~/.bashrc. Most of the personalization options can be set by editing this file. Most customizations can be a hassle because we often need to set options that change files mentioned or not mentioned above. The Bash environment is highly customizable precisely to suit the different needs of different users.

When the login shell exits normally, ~/.bash_logout and /etc/bash_logout will be called if they exist. The next figure shows Bash starting as an interactive shell. For example, when a user opens a terminal emulator from the desktop environment, this is done in the following order.

We already know that Bash executes different commands under different startup modes, so it’s clear that there are only a few typical startup modes that need the most attention. Non-interactive and interactive login shells, and non-interactive and interactive non-login shells, respectively. If you want to define some global environment, you need to place a file with a unique name and a.sh suffix (such as custom.sh) in the /etc/profile.d directory.

Special attention should be paid to non-interactive non-login startup modes. In this mode, Bash checks the BASH_ENV variable. If the variable is defined, Bash refers to the file it points to. In addition, the value stored in the PATH variable is not used when handling BASH_ENV, so it must contain the absolute PATH to the execution file. For example, if one wants the shell to read the Settings in the ~/.bashrc file when executing the script non-interactively, one could put something like the following in a file called /etc/profile.d/custom.sh…

# custom.sh.# If Fedora Workstation is used
BASH_ENV="/home/username/.bashrc".If Fedora Silverblue Workstation is used
BASH_ENV="/var/home/username/.bashrc"

export BASH_ENV
Copy the code

The above script causes each shell script to execute the user’s ~/.bashrc before running.

Users typically customize their system environment to fit their own work habits and preferences. For example, users can achieve this level of customization through aliases. Commands that have the same starting parameters and need to be used frequently are the best choices for making aliases. Some of the aliases defined in the ~/.bashrc file are shown below.

# .bashrc
Execute global files
if [ -f /etc/bashrc ];
   then . /etc/bashrc
fi.User aliases and functions
alias ls='ls -hF --color=auto'
alias la='ls -ahF --color=auto'

Make the dir command work like it does on Windows
alias dir='ls --color=auto --format=long'

# Grep result with color highlight
alias grep='grep --color=auto'
Copy the code

In the system, an alias is a way of customizing various commands. It reduces keystrokes and makes commands easier to use. Aliases for a user level are usually stored in the ~/.bashrc file for that user.

If you find yourself constantly searching history for a command that was executed, you might want to change your history Settings. You can still set history options for the user level in the ~/.bashrc file. For example, if you are used to using multiple terminals at the same time, you might want to enable the HISTAppEnd option. Some bask-related shell options are Boolean in nature (receiving on or off) and can often be enabled or disabled with the built-in command shopt. Bash options that accept more complex values, such as HISTTIMEFORMAT, are often configured by assigning values to environment variables. The following shows how to customize Bash with shell options and environment variables.

# Configure Bash history

# TAB the directory environment variable and set histAppEnd
shopt -s direxpand histappend

# ignoreboth is equal to ignorespace and ignoredup
HISTCONTROL='ignoreboth'

Control the time format in the 'history' output
HISTTIMEFORMAT="[%F %T] "

# Unlimited history
# NB: In the new version of Bash, anything < 0 will work, but in CentOS/RHEL, only this will work
HISTSIZE=
HISTFILESIZE=

Or for those of you using the new version of Bash
HISTSIZE=-1
HISTFILESIZE=-1
Copy the code

The direxpand option in the above example lets Bash replace the directory name with the result of the word expansion when the file name is completed. It changes the contents of the Readline edit buffer, so whatever you typed has been replaced by the completion result.

The HISTCONTROL variable is used to enable or disable certain filtering options for command history. Repeated lines, lines that start with blank space, can be filtered out of the command history by this option. Quoting from Dusty Mabe, this is a tip I got from him:

Ignoredup lets history not record duplicate entries (if you execute the same command over and over again). Ignorespace ignores blank entries before it, which is useful when setting an environment variable that contains sensitive information or when executing a command that does not want to be recorded on disk. Ignoreboth is a combination of these two options.

Dusty Mabe — principal software engineer, Redhat, June 19, 2020

For command-line heavy users, Bash has a CDPATH environment variable. If CDPATH contains a list of directories to be searched by the CD command and provides a relative path as the first argument, it checks all the listed directories in order, looking for the matching subdirectory and switching to the first matching result directory.

# .bash_profile

# set CDPATH
CDPATH="/var/home/username/favdir1:/var/home/username/favdir2:/var/home/username/favdir3"

I can also write my # like this
CDPATH="/:~:/var:~/favdir1:~/favdir2:~/favdir3"

export CDPATH
Copy the code

CDPATH is usually updated in the same way as PATH — preserving the original value by referring to itself on the right side of the assignment.

# .bash_profile

# set CDPATH
CDPATH="/var/home/username/favdir1:/var/home/username/favdir2:/var/home/username/favdir3"

Or I could write it like this
CDPATH="/:~:/var:~/favdir1:~/favdir2:~/favdir3"

CDPATH="$CDPATH:~/favdir4:~/favdir5"

export CDPATH
Copy the code

PATH is another extremely important variable. It is the search path for commands on the system. Note that some applications require their own directories to be added to the PATH variable in order to work properly. As with CDPATH, append the new value to the PATH variable by referencing the original value to the right of the assignment. If you want to put the new value first, just put the original value ($PATH) at the end of the list. Also note that in Fedora, this column of values is separated by a colon (:).

# .bash_profile

# Add PATH value to PAHT environment variable
PATH="$PATH: ~ / bin: ~ : / usr/bin: / bin: ~ / JDK - 13.0.2: ~ / apache maven - 3.6.3"

export PATH
Copy the code

Command prompt is another popular custom option. It has seven customizable parameters:

  • PROMPT_COMMAND: If set, it will be displayed at every main prompt ($PS1) before it appears.
  • PROMPT_DIRTRIM: If set to a number greater than zero, this value is used for expansion\w\WNumber of trailing directory components retained when a prompt string is escaped. Deleted characters are replaced with ellipses.
  • PS0: The value image of this parameterPS1To be displayed after the interactive shell reads the command but before executing it.
  • PS1: Main prompt string. The default value is\s-\v\$.
  • PS2: secondary prompt string. The default is>. Before I show it,PS2PS1It unfolds like that.
  • PS3: The value of this parameter is used asselectCommand prompt. If this variable is not set,selectCommand will use#?As a prompt.
  • PS4: The value image of this parameterPS1That way, if I set it-xOption, which is displayed as a prompt before the command line is echoed. The first character of the expanded value is copied several times as necessary, indicating the level of indirection. The default value is+.

Bash Reference Documentation 5th edition, Bash version 5.0 May 2019

This single aspect of Bash can be discussed throughout this article. You can find lots and lots of information and examples. Examples of dot files, including prompt reconfiguration, are provided in the repository linked at the end of this article. Feel free to use the examples in the repository to learn and experiment.

conclusion

Now that you know something about how Bash works, you can easily modify your Bash dot file to suit your own needs and habits. Beautify your prompts and make aliases so that your computer really belongs to you. Look at the contents of the /etc/profile, /etc/bashrc, and /etc/profile.d/ files for some inspiration.

You can also write comments about terminal emulators here. There are many ways to configure your favorite terminal exactly as you want it to be. You may have already thought of it, but it can usually be done by… HMM… Dot files in the user’s home directory do this. The terminal emulator can also be started as a login session, and some people always prefer a login environment. The way you use the terminal and the computer depends on how you modify (or don’t modify) your dot file.

If you’re wondering what session state your command line is in, use this script to find out.

#! /bin/bash
case "$-" in
   (*i*) echo This shell is interactive ;;
   (*) echo This shell is not interactive ;;
esac
Copy the code

Put these lines in a file, add executable permissions, and run to see what type of shell you are currently in. $- is a variable in Bash that, in the case of an interactive shell, contains the letter I. Alternatively, you can simply print the $- variable and check whether its output contains the I flag.

$ echo $-
Copy the code

The reference information

Refer to the following resources for more information and examples. The Bash manual is also a good source of information. Be sure your local man page records the features of the current version of Bash you’re running, as information found online can sometimes be too old (out of date) or too new (your system hasn’t been installed yet).

  • Opensource.com/tags/comman…
  • Opensource.com/downloads/b… (On the site, you must enter a valid email address or sign up to download.)
  • Opensource.com/article/19/…

Community members who have contributed to this article in various forms (dot file examples, hints, and script files) :

  • Micah Abbott — Chief Quality Engineer
  • John Lebon — Chief Software Engineer
  • Dusty Mabe — Chief Software Engineer
  • Colin Walters — Senior Principal Software Engineer

Sample dot files and scripts can be found in this repository:

  • Github.com/TheOneandOn…

Check the information in the repository provided above. Some may be outdated. It also contains many custom scripts and pet container configuration examples in development, which are not dot files. I recommend starting with John Lebon’s dot files, which are fully explained from start to finish. They are the most detailed I’ve seen and contain very good descriptions. Have fun!


Via: fedoramagazine.org/customizing…

By Stephen Snow: lujun9972

This article was originally compiled by LCTT and released with honor by Linux China