Skip to main content
X

Send us a Topic or Tip

Have a suggestion for the blog? Perhaps a topic you'd like us to write about? If so, we'd love to hear from you! Fancy yourself a writer and have a tech tip, handy computer trick, or "how to" to share? Let us know what you'd like to contribute!

Thanks for reaching out!

Tech 101: Introduction to the Mac’s Terminal App, Part Two

TerminaliconLast week we kicked off our Introduction to the Mac’s Terminal App with a quick look at using and customizing Terminal, using the Bash shell to run a few commands; in our example, we used Terminal to modify the Mac’s Dock.

In this installment we’ll delve deeper into the Bash shell, including creating a basic shell script, and looking at a few more commands to customize your Mac.

Login Scripts
Terminal runs a set of scripts when a Bash shell is launched. By default, Bash shells in the Mac OS are of the login type, and run two specific scripts. The first is a system wide script that is run regardless of who is currently logged in. The second script is located at ~/.bash_profile, and is run only for the specific logged-in user, and then only if a Bash shell is being started.

There’s another set of scripts that are used when a non-interactive Bash script is run. These are scripts you create that begin with #! /bin/bash. This is the type of Bash shell script we mentioned that we would explore a bit later.

Just like the login scripts, there’s a bashrc script that is system wide, and another at the user level, located at ~/.bashrc.

The bashrc script, as well as the login profile scripts, allows you to customize the Bash shell to meet your needs.

(The shell's prompt can be customized to meet your needs. Here I've added a time stamp to my Bash shell prompt.)
(The shell’s prompt can be customized to meet your needs. Here I’ve added a time stamp to my Bash shell prompt.)

Change Bash Shell Prompt
One of the first customizations of the Bash shell that is usually performed is to change the default shell prompt to something more personal. To do this we’ll use the script located at ~/.bash_profile. We’ll add a command to the script that will change how the Bash shell prompt appears.

The command we’ll insert is:

PS1=” ”

Between the quotes is where the customization of the prompt occurs. You can use the following parameters (there are actually many prompt escapes you can use, but these should be enough to get you started):

\d – Current date

\t – Current time

\h – Host name (your Mac’s name)

\# – Command number

\u – Logged-in user name

\W – Current working directory

\w – Current working directory with full pathname

My custom prompt is PS1=”\t \h \u \W $ “. Note the space after the $ symbol; it’s not a requirement, but it adds a single character space between any command you type in Terminal and the $ prompt, for easier readability.

Adding that line to the ~/.bash_profile file can be done from Terminal, or with a text editor, such as TextWrangler, an easy-to-use editor for coding. To add the PS1 command using Terminal and the built-in nano editor, try the following:

(The nano editor opens within Terminal, and provides basic text editing functions.)
(The nano editor opens within Terminal, and provides basic text editing functions.)

Launch Terminal, located at /Applications/Utilities/.

At the Terminal prompt, enter the following:

nano .bash_profile

Press enter or return.

The nano editor will open in Terminal. Enter the following, with no return or enter:

PS1=”\t \h \u \W $ ”

Press Control + O. A dark banner will appear at the bottom of the nano editor, asking you to confirm that you wish to write the .bash_profile file. Press enter or return.

Press Control + X to exit nano and return to Terminal.

To see your new custom prompt, select Shell, New Window.

A new Terminal window will open that will include your new custom prompt.

There are many more ways to customize the Bash shell, but this is a good example to get you started.

Creating Bash Shell Scripts
Scripts have been a part of the Mac for ages. Your Mac comes equipped with multiple scripting systems, including AppleScript and Automator. The Bash shell can also run scripts. Bash shell scripts can be as simple as a series of commands you often use, or just about as complex as you can imagine.

texteditscript1280

Bash shell scripts are written in plain text and always begin with a shebang. A shebang is a two-character sequence that all shell scripts must start with. The pound sign (#) and exclamation point (!) together are called the shebang (#!). The shebang tells the shell that what follows the shebang is a directive instructing the computer what program interpreter to use, and that the rest of the lines following the shebang are a program to be run through the interpreter.

Our script will look like this:

#!/bin/bash

First_Argument=$1

Echo Hello world $First_Argument

This is a pretty standard first scripting program. It takes a user-supplied variable (First_Argument=$1) and supplies it to the echo command, which will print out in Terminal Hello world, plus whatever the value of $First_Argument is.

Let’s try it out.

Launch TextEdit.

Enter the script above into the document.

Select Make Plain Text from the Format menu.

Save the document as MyFirstScript in your home directory.

Make sure the option “If no extension is provided use .txt” is unchecked.

Click the Save button.

The MyFirstScript file is just a plain text file at this point. To make it into a Bash shell script, it needs to be marked as an executable file. You do this by using the chmod command to change file permissions.

In Terminal, make sure you’re in your home directory:

cd ~

chmod 700 MyFirstScript

To try out the script, enter the following at the Terminal prompt:

./MyFirstScript leaders

The ./ tells Bash the script is in the current directory, and the word leaders after the script name is the Argument you’re passing to the script.

You should see Terminal respond with:

Hello world leaders

Terminal Command Tips
Terminal and Bash have some nifty tricks for reducing the amount of typing you need to perform. Two of our favorite tricks are:

(The history command will display past shell commands, numbered in the order you used them. Check the bonus tip for how you can use history.)
(The history command will display past shell commands, numbered in the order you used them. Check the bonus tip for how you can use history.)

Up arrow: Repeats the last command you entered. You can use the up arrow multiple times to go back through the history of commands. This way, you can redo any command that’s in the Terminal history file without having to type it back in.

history: The history command will display all the commands you’ve run. If you provide a number after history, you can display the last x number of commands. Bonus tip: Command history includes numbers. If you want to rerun command number 4, enter !4 at the prompt.

Mac OS Defaults
One of the uses of Terminal and the Bash shell is to read or write an app’s preference settings. Many times apps have preferences that aren’t available via the standard GUI interface.

To finish off our introduction to Terminal, we’re going to end with some of our favorite defaults commands to customize your Mac.

(Terminal can be used to set the Save or Save As dialog boxes to open in the expanded state, making it easier to browse through the file system.)
(Terminal can be used to set the Save or Save As dialog boxes to open in the expanded state, making it easier to browse through the file system.)

Expand Save Panels
Execute the following command in Terminal to force the Save panel to always show the expanded view:

defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode -bool true

To undo, use:

defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode -bool false

Display Hidden Files

The Mac OS hides some files from the user. This command will make them visible in the Finder.

defaults write com.apple.finder AppleShowAllFiles TRUE

Press enter or return.

Killall Finder

Press enter or return.

To undo, use:

defaults write com.apple.finder AppleShowAllFiles FALSE

Press enter or return.

Killall Finder

Press enter or return.

Add a Login Message
If you have your Mac configured to require a login on startup, then this command will let you add a message to the login window.

sudo defaults write /Library/Preferences/com.apple.loginwindow LoginwindowText “Your login window message text goes here”

Press return or enter.

You’ll be asked for an administrator’s password.

Press return or enter.

Resources
For additional Terminal and Bash resources, check out the following:

Tom Nelson
the authorTom Nelson
Writer
Tom has been an enthusiastic Mac user since the Mac Plus. He’s also been known to dabble in the dark side, otherwise known as Windows, and has a well-deserved reputation for being able to explain almost anything to anybody. Tom’s background includes more than 30 years as an engineer, programmer, network manager, software tester, software reviewer, database designer, and computer network and systems designer. His online experience includes working as a sysop, forum leader, writer, and software library manager.
Be Sociable, Share This Post!

Leave a Reply

2 Comments