Jump to content

Bash Shell Scripting/Some Notes On Your Shell

From Wikibooks, open books for an open world

Shell version

[edit | edit source]

This textbook provides information about commonly-available versions of Bash; as of this writing, most systems will have Bash version 5.x. Older systems may still have Bash 4.x. In casual usage, you are unlikely to notice the differences, but you can read the release announcement for Bash 5.0 if you are curious.

In years past, macOS, formerly OS X or Mac OS X, shipped with an outdated version of Bash, in the 3.2 series.[1] Nowadays, the default shell for macOS is Z shell. Many concepts and commands that apply to Bash apply equally well to Z shell, but you should be aware that there are differences, in particular with the Z shell scripting language. This, however, is a guide for Bash.

On a Mac, if you wish to use the latest Bash shell, you can look into MacPorts or Homebrew, both of which provide Bash and other Unix utilities in precompiled forms for all recent Mac operating systems. As a last resort—or if you're simply feeling adventurous!—you can fetch the source code from https://www.gnu.org/software/bash and compile a recent Bash yourself. On a Mac, this will require the command-line tools for Xcode.

Shell startup files

[edit | edit source]

Upon startup, Bash reads in and executes the contents of one or more hidden files, or "dotfiles", which initialize the new shell or login session. These are ~/.bashrc and ~/.bash_profile or ~/.profile, where ~ is a shorthand in Bash for "the user's home directory". In a Unix environment, files whose names begin with a period are hidden by default, so they don't appear in listings; use ls -a to see them. There are also system-wide initialization files in /etc, which affect all users' login sessions.

The topic of initialization files for Bash is somewhat complex, but the out-of-the-box configuration on most systems using Bash as the default shell should suffice for the rest of this guide. A new user on most GNU/Linux systems will have Bash set to be the login shell, and properly-configured .bash_profile and .bashrc configuration files will likely already be present in their home directory. As a beginner, you need not be very concerned with the contents of these files. However, both can be inspected and changed with a text editor like nano, as with most configuration files in Unix/Linux environments.

If you're curious about the details, it's best to refer to an authoritative reference for a thorough explanation of the differences between the various shell startup files, such as the "Bash Startup Files" section of the Bash manual. This explanation from the Beyond Linux From Scratch project is also good.

Here are some example Bash initialization files:

Notes

[edit | edit source]
  1. The consensus seems to be that that Apple were unhappy with a licensing change to later versions.
  2. https://www.linuxfromscratch.org/blfs
  3. https://tldp.org/guides.html#abs or read online at https://tldp.org/LDP/abs/html