Sunday, 24 November 2013

Updating Bash

A shell allows you to run commands to interact with your computer, it's the shells job to understand what you have written and to perform an action based on it. When you type "date" for example, the shell looks in all the locations it has stored for an executable called "date", runs it, and shows you the output.

There are various shells that work slightly differentially, but the one that comes default in OS X is Bash. By default OS X mavericks runs bash 3.2, we're going to update it to the latest version 4.2.

Prerequisites 

Before attempting this tutorial you'll need to have homebrew installed.


This tutorial was done with the following versions:
  • Mavericks (10.9)
  • Homebrew (November 2013)

Installation

1. Open a terminal and install bash
brew install bash

Homebrew is pretty easy

Wait until bash has finished installing

Now the latest version of bash is installed, but you still have to configure some things to get your terminal to run the latest version at startup. We need to tell a configuration the location of new version of bash, and we need to run a command to actually change the shell.

Configuration

1. Use this one liner to add "/usr/local/bin/bash" to the end of this file "/etc/shells"

sudo bash -c "echo /usr/local/bin/bash >> /etc/shells"
A few extra steps are required when permissions are needed in directions.

2. Check the file with this command to confirm it was written correctly

cat /etc/shells
/usr/local/bin/bash

The command "chsh" which we'll use in a minute, won't allow you to pick a shell that isn't in this list.


3. Write the following into the terminal, followed by your password to change the default shell.

chsh -s /usr/local/bin/bash

chsh -s /usr/local/bin/bash


4. Close and reopen your terminal

5. Write the following commands in to check if the new shell is running

echo $SHELL
echo $BASH_VERSION

$SHELL and $BASH_VESRION are both environment variables.

End Notes 

Check out some other tutorials about OS X and the terminal.

  • Click here to learn how to add color to your bash prompt (coming soon)
  • Click here for a list of awesome command line utilities (coming soon)

Tuesday, 12 November 2013

Making "ls" command display color

In an effort to make a terminal in OS X behave more like a terminal would in linux this quick tutorial will make the command "ls" always display color when displaying directories content. This is helpful for quickly identifying what type of files we're dealing with, and generally just makes using the terminal more visually appearing.

Prerequisites

Before attempting this tutorial you'll need to have coreutils installed.

This tutorial was done with the following versions:
  • Mavericks (10.9) 
  • GNU coreutils 8.21

Installation

1. Enter the following in a terminal
echo alias ls="'ls --color'" >> ~/.bash_profile
Note the double quotation marks. These are needed otherwise they're ignored by the echo command.


An easy 1-liner

The alias command tells the shell to run an alternative command in place of a string, in this case, it's running "ls --color" everything the string "ls" is entered. The echo command redirecting into the .bash_profile file just confirms that the shell runs this command every time it's started.

2. Restart the terminal

3. Perform the command to ensure it has worked.

ls

Glorious Color 

End Notes

Check out some other tutorials about diving into the terminal side of things in OS X.

Updating Coreutils (Homebrew)

Coreutils (GNU) are a set of basic utilities that most unix-like operating systems come with. Although OS X comes with most of them, some are very outdated and lack features we might want to use. One example of a feature we want is using the ls command (lists directory contents) to show colours based on the type of file.

Prerequisite

This tutorial is based on the assumption homebrew is installed

This tutorial will be used with homebrew but can be applied to other package managers (such as MacPorts) following the basic structure:

  • Install Coreutils
  • Change PATH environment variable to check coreutils directory first
  • Restart terminal

This tutorial was done with the following versions:
  • Homebrew (November 2013)
  • Mavericks (10.9)

Installation

1. Open up your preferred terminal

2. Copy this into your terminal and press enter
brew install coreutils

coreutils installing


3. Run the following command 
gls --color

Pretty colors !


What you're doing here is running the "ls" command with the option --color. Because Homebrew doesn't want to be destructive to your system files and overwrite the ones that come with OSX, it sticks them in another folder and creates an alias of each command with the character "g" in front of it so you can access it.

If you're comfortable using the updated coreutils like this then you can stop this tutorial. If you're like me and wish to use them instead of writing "g" in front of the command every time, continue.

Configuration 


1. Run the following command
echo export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" >> ~/.bash_profile

2. now run another command
echo export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH" >> ~/.bash_profile

Adding environment variable to your .bash_profile


What we've done here is basically told the shell to look in the directory of the updated utils before anything else. We've also told it to look in the man directory (for man pages) of the updated utils before anything else. For further explanation of the above command I encourage you to look into the following things.

  • echo
  • environment variables
  • redirection 
  • .bash_profile

3. Close and Open your terminal for the changes to take effect

4. Enter the following command
man ls  
5. Scroll to the bottom of the page (ctrl + g) and see that you're running an up-to-date version.

GNU Coreutils 8.21

End Notes

Check out some other tutorials about diving into the terminal side of things in OS X.



Tuesday, 29 October 2013

Installing Command Line Tools (Mavericks)

Installing Command Line Tools is useful for running package managers such as MacPorts and HomeBrew on OS X. Apple recently released it's latest operating system 10.9 - Mavericks and it's now even easier to install Command Line Tools, in-fact you don't even need Xcode to install them.


Prerequisites

This tutorial was done with the following versions:

  • Mavericks (10.9)


Installation 

1. Open up your preferred terminal.

My preferred terminal - iTerm2


2. Copy this into your terminal and press enter

xcode-select --install

3. Click install on the next prompt

Install Prompt

4. Accept the license agreement

5. The installer will automatically download and install

Installer doing its thing

6. You now have Command Line Tools installed.

The software was installed. :) 


There are other ways of installing Command Line Tools. You can directly download them from Apples developer website. https://developer.apple.com/downloads/index.action


End Notes

You can now build and compile software on OS X! You're free to also download and use a package manager of your choice.




Thursday, 17 October 2013

Installing Homebrew

A Package Manager is used to manage software on an operating system. Although OS X comes with it's own package management (The App Store) it's limited to Apples restrictions. There are currently several popular package managers for OS X based on open-source software that are usually ports from unix/linux systems. The popular ones include MacPorts, Homebrew, Fink and pkgsrc. Today we will be installing Homebrew and an example application matrix.


Prerequisites

Before Attempting this tutorial it is useful to have X11 and Command Line Tools installed


This tutorial was done with the following versions: 
  • Mountain Lion (10.8.5)
  • Homebrew (Version as of 18 October 2013)

this tutorial should also work on 10.9 Mavericks.

While it's possible to have 2 package managers installed on the one system, it's not recommended. You should pick either Homebrew or MacPorts, don't install both unless you know what you're doing.


Installation

1. Open up your preferred terminal. 

My preferred terminal - iTerm2


2. Copy this into it and hit enter
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"

3. Press enter to continue

Press enter before continuing installation


4. Enter your password to proceed. Homebrew will install.

Enter your password

5. Homebrew is now installed. It's that simple.


Brew Doctor 

Homebrew comes with an awesome feature to check if there is any potential problems when using it. 

Run this in your terminal to check for problems
brew doctor
Your System is ready to brew

Sometimes brew doctor will give you warnings about files already in your /usr/local directory (the directory that Homebrew uses). Files can be put their based on other things you've installed on OS X. For example. I have "Paragon NTFS 9" installed on my Mac (a program that allows write access to NTFS drives) which has installed some files to the /usr/local directory. I can go ahead and ignore these warnings because I know why these files are here.

Brew Doctor Warnings

Installing cmatrix

Installing applications in Homebrew is fairly easy. Write the following into your terminal:
brew install cmatrix
Installing an application in homebrew

This command checks the Homebrew repository for an application called cmatrix (the repository was installed by default when you installed Homebrew) and then downloads and installs the application.

We can now run the application from the terminal, type:
cmatrix
The application "cmatrix" running


End Notes

Now that you have a package manager installed you download and use thousands of applications.




Wednesday, 16 October 2013

Installing Command Line Tools (Mountain Lion)

Even though you may not be developing applications, all of the popular package managers (such as MacPorts and Homebrew) require you to have "Command Line Tools" installed to be able to build and compile packages. Xcode is Apples IDE for developing software and is the easiest way to install the tools you need for these package managers.

Prerequisites


Note: This tutorial is obsolete if you're running 10.9 - Mavericks. Please use this tutorial instead. Installing Command Line Tools (Mavericks)

This tutorial was done with the following versions:

  • Mountain Lion (10.8.5)
  • Xcode (5.0) 


Installation


1. Download Xcode form the App Store (you will need an Apple ID for this)

Downloading Xcode from the App Store

2.  Launch Xcode

Starting Xcode from Launchpad

3. You'll need to agree to the terms and conditions and enter your password 

Agree to the Licence Agreement

4. Once Xcode has started. From the menubar select
Xcode -> Preferences

Enter Xcode Prefrences

 5. This will bring up the preferences window. Across the top are different menus tabs. It defaults to "General". Click "Downloads".

Preference Window -"General" tab.

7. Once in the "Downloads" menu. Click the little arrow next to "Command Line Tools" to start the download.

Downloading "Command Line Tools"

8. Once the Download / Installation is finished a tick will appear and you're now free to close Xcode.

"Command Line Tools" are installed.

End Notes

You can now build and compile software on OS X! You're free to also download and use a package manager of your choice.





Installing XQuartz


A windowing system is used by an operating systems to draw and move windows and other GUI elements. The default windowing system on OS X is the Quartz Compositor. The X Window System (X11) is a common windowing system for unix/linux operating systems that was packaged in OS X until Mountain Lion (OS X 10.8). 

X11 is needed for running certain programs (Wireshark) and window managers (i3, awesome) that where built for it. Luckily we can install the now open source version of XQuartz to do so.

Prerequisites

This tutorial was done with the following versions: 
  • Mountain Lion (10.8.5)
  • XQuartz (2.7.4)
This method should also work on OS X 10.9 - Mavericks.  


Installation


     1. Downlaod XQuartz from http://xquartz.macosforge.org/landing/

     2. Mount (open the dmg)

Mount XQuartz.dmg

     3. Run XQuartz.pkg

Run XQuartz.pkg


     4. Continue through the setup (defaults are fine)

Defaults are fine for installer

     5.  The installer will prompt you about logging out and in again to make XQuartz your default X11 server. Click OK

Message by installer

     6. Once the installer has finished. Logout and Log back in again.

     7. Start XQuartz

Lunchpad to open XQuartz

     8. You are now running the X Window System on OS X. xterm (a terminal application that runs on X11) will open by default.

xterm window


Additional Information

To see other examples of applications running on X11 type the following into xterm:
  • xeyes
  • xclock
  • xedit
  • xlogo
  • xcalc
To stop these applications press "ctrl + c" in the xterm window

All of these programs come bundled with XQuartz and are just some examples of programs that you can run using the X Window System.

End Notes

We can do some fun things now that we can X11 installed