Day 40: Operating Systems… Not Today

I got down to the root of my issue with my config files. I am lacking a better understanding of Operating Systems and how Virtual Machines and the like operate. With that knowledge I decided to limit what I look into. Generally I try to find the route of my misunderstanding but in this case I will settle with know the execution order of regular Bash shells on Linux and how to configure my own Git Bash shell.

TLDR;

Okay, so here are the highlights of what I did:

  • I looked into some books on how operating systems work and what they do. I was also curious about how really small devices like thermostats work. I had to stop myself because I have already significantly strayed away from my current learning objective (Learning how to operate and work within the shell with a good amount of it’s features)
  • I learned more about how config files work in Bash and the execution order. I still haven’t been able to trace where all of the config file calls are coming from which irritates me but I just have to accept it for now. I will eventually come back to this topic when I want to more intricately customize my terminal.

Notes on Bash Config Files Execution Order


The PS1 variable allows for special character to be interpreted as unique value when read. This is different compared to other variables being able to use these values (I think…?).

  • ‘Jamal Ahmed’ = ‘\u’ (You can check this by running whoami in the terminal)
  • ‘DESKTOP-SSD90TR’ = ‘\h’ (You can check this by running hostname in the terminal)
  • $MSYSTEM = The name of the computer system… I think (To learn more I need to better understand MinGW and MSYS2 which falls under learning more about operating systems. This is a bit too far out of the scope for these notes so I will leave it there.

Here is a list of some of the other special characters that can be used within the PS1 variable:

SPECIAL CHARACTERS

  • \a Inserts a C-g character, which makes the internal speaker beep. (It “rings the system bell”; C-g is sometimes called the bell character.)
  • \d The current date.
  • \h The hostname of the system.
  • \n A newline character.
  • \t The current system time, in 24-hour format.
  • \@ The current system time, in 12-hour a.m./p.m. format.
  • \w The current working directory.
  • \u Your username.
  • \! The history number of this command.

Order of Execution for Shell Config Files

There are a number of configuration startup files in your home directory that you can edit to make your configurations permanent. You can also edit these files to specify commands to be run whenever you first log in, log out, or start a new shell. These configuration files are text files that can be edited with any text editor (see Text Editing).

When you log in, bash first checks to see if the file /etc/profile exists, and if so, it executes the commands in this file. This is a generic, system-wide startup file that is run for all users; only the system administrator can add or delete commands to this file.

Next, bash reads and executes the commands in .bash_profile, a “hidden” file in your home directory. So to make a command run every time you log in, add the command to this file.

For all new shells after you’ve logged in (that is, all but the “login shell”), bash reads and executes the commands in the .bashrc file in your home directory. Commands in this file run whenever a new shell is started except for the login shell.

There are separate configuration files for login and all other shells so that you can put specific customizations in your .bash_profile that only run when you first log in to the system. To avoid having to put commands in both files when you want to run the same ones for all shells, append the following to the end of your .bash_profile file:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

This makes bash run the .bashrc file in your home directory when you log in. In this way, you can put all of your customizations in your .bashrc file, and they will be run both at log in and for all subsequent shells. Any customizations before this line in .bash_profile run only when you log in.

For example, a simple .bash_profile might look like this:

# "Comment" lines in shell scripts begin with a # character.
# They are not executed by bash, but exist so that you may
# document your file.

# You can insert blank lines in your file to increase readability;
# bash will not mind.

# Generate a welcome message when you log in.
figlet 'Good day, '$USER'!'

# Now run the commands in .bashrc
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
This `.bash_profile' prints a welcome message with the figlet text font tool (see Horizontal Text Fonts), and then runs the commands in the `.bashrc' file.

A simple .bashrc file might look like this:

# Make color directory listings the default.
alias ls="ls --color=auto"

# Make "l" give a verbose directory listing.
alias l="ls -l"

# Set a custom path.
PATH="/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:~/bin:."

# Set a custom shell prompt.
PS1="[\w] $ "

# Make a long history list and history file.
HISTSIZE=20000
HISTFILESIZE=20000

# Export the path and prompt variables for all
# variables you define.
export HISTSIZE HISTFILESIZE PATH PS1

This .bashrc sets a few useful command aliases and uses a custom path and shell prompt whenever a new shell is run; with the preceding .bash_profile, this .bashrc is also run at login.

When you log out, bash reads and executes the commands in the .bash_logout file in your home directory, if it exists. To run commands when you log out, put them in this file.

To clear the screen every time you log out, your .bash_logout would contain the following line:

clear

This executes the clear command, which clears the screen of the current terminal, such as in the xterm window where you type it, or in a virtual console.

NOTE: Some distributions come with default shell startup files filled with all kinds of interesting stuff. Debian users might want to look at the example startup files in /usr/share/doc/bash/examples/startup-files.


Conclusion

That’s all for today. If you are interested in the MIT course you can check out the video lecture I’m currently going through. The lecture is helpful but isn’t sufficient by itself. Anyways, until next time PEACE!