Getting started with Bash

From SwinBrain

Operating systems need to provide a means for users to interact with the files and programs that exist within the computer. Modern operating systems provide a graphical user interface such as Aqua, GNOME, KDE and Windows that can be used to perform these operations. These graphical interfaces make performing many actions with the computer simpler, lowering the level of expertise needed to operate a computer.

Historically, computers used a command line environment into which users entered text to instruct the computer to perform operations. While this means of interacting with the computer is no longer necessary, the command line can be more convenient to perform a range of actions. An understanding of the use and operation of a computer via the command line is an essential skill for IT professionals looking to work in technical areas.

This article discusses the basic things you need to understand to get started working with the command line using the Bash shell.

Contents

What is a Shell

A shell is a program that is used to interpret instructions from the user: a command line interpreter. This program reads text entered by the user, and uses this text to perform actions within the current context. While replaced by graphical user interfaces for many users, these command line interpreters provide a convenient and powerful interface that can be used to perform many actions in an efficient manner.

The Unix Shell on wikipedia provides more information on the concept and various categories of shells in Unix.

What is Bash

Screenshot of bash and sh sessions from wikipedia

The Bourne shell was provided with early Unix operating system from which Bash takes its name. The name Bash is an acronym standing for Bourne-Again SHell, a play on words for the phrase born again. Bash is an open-source shell that provides a superset of the Bourne shell commands and is the default Shell program on many Linux operating systems and MacOSX. Bash has been ported to Windows in MSYS and Cygwin.

Bash on Linux and MacOS

Launching the Terminal on Ubuntu

In Unix variants, such as Linux and MacOS, the Terminal program can be used to interact with Bash, the default shell program on these platforms. In these operating systems you can start using Bash by launching this Terminal program. When Terminal launches you should be presented with a command prompt into which you can enter commands.

A terminal window in Ubuntu (using the Emerald window decorator with a Aqua like theme)

Bash on Windows

In order to use Bash on Windows you need to install a suitable Bash interpreter and Terminal program. The simplest way of achieving this is to install the MSYS program. The download page on the MinGW web site contains a number of packages including the MSYS Base System. Download and install this, for example the mingw-get-inst-20110211 installer will download and install the current version.

Install the MSYS Base System

Run the mingw-get-inst-20110211 installer and follow the prompts. You will be asked which MinGW components you want to install, make sure you select the MSYS Base System.

Launch the MinGW Shell
The installer will have created a MinGW option in the start menu. Use the MinGW Shell link to open the terminal. When Terminal launches you should be presented with a command prompt into which you can enter commands.

MSYS terminal used for running Bash on Windows.

Bash Commands

Windows organise its file system into drives. In MSYS these drives are located in a single root file system. This means you can find c: at /c/ and d: at /d/, so the C:\temp\lab folder is accessed as /c/temp/lab in MSYS.

The file system is one of the first things you need to understand when getting start with UNIX. The file system is part of the operating system used to store and organise files. These files are organised into hierarchical directories, or folders. At the base of this hierarchy is the root of the file system. In Unix the root is signified by the symbol /.

When you open a terminal, the command interpreter sets up a context (or environment). This context includes a current directory. The interpreter will use this current directory when you want to access files. For example if you are currently in the /users/fred directory, the file hello.txt would refer to the file /users/fred/hello.txt as the shell program will use the current directory when locating the file. You can always access files by entering their full path, but often it is easier to use the current directory to make it easier to access the files you want to work with.

Note: For more information on UNIX filesystems, directories and permissions, see the Introduction to file systems in UNIX SwinBrain article.

Changing the current directory

The first command you need to learn is the command to change the current directory. This is the cd command. When you run this command you pass it the path to the new directory. The following example illustrates the use of the cd command to move through various directories.

#Move to the root of the file system
cd /
 
#Move to the temp folder
cd /tmp
 
#Move to c:\temp in MSYS
cd /c/temp
 
#Move to your home directory
cd ~
 
#Move to the code folder in the fred folder in users on a Mac
cd /users/fred/code
 
#Move to the leet folder in the fred folder in home on Linux
cd /home/fred/leet
 
#Move up a directory
cd ..
Note: The cd command by default will return to the home directory of the current user. So 'cd' is equivalent to 'cd ~'
Note: Files and directories in UNIX are case sensitive. The /tmp and /Tmp directories are different...

The pwd command can be used to print the current directory (also known as the working directory - pwd stands for print working directory).

#Print the path of the current directory (working directory)
pwd
 
#move then print where we are...
cd ~/..
pwd

The following links refer to the manual pages for these commands:

Listing Files

The ls command can be used to list the details of files and directories. The ls command has a number of optional flags you can pass to configure the output generated.

#List the files in the current directory
ls
 
#List the files in the root directory
ls /
 
#List the files in your home directory
ls ~
 
#List the files in c:\temp\mycode in MSYS
ls /c/temp/mycode
 
 
#List the files in the current directory showing all files in a vertical list
ls -al
 
#List all files in the root directory in a list with human readable sizes
ls -lha /

The following links refer to the manual pages for these commands:

Copying, Moving, Renaming and Deleting Files and Directories

There are a number of commands related to manipulating files in any UNIX shell, such as Bash. The cp command is used to copy files, the mv command to move them and the rm command to remove (delete) them. In a UNIX environment, the process of renaming a file consists of 'moving' it from the old name to the new one.

The cp and mv commands are passed two paths to a file. The first is the source, the file to move, rename or copy, and the second is its destination, or new name.

#copy hello.txt from the current directory to the root directory
cp hello.txt /
 
#move hello.txt from the current directory to the /c/temp/lab directory
mv hello.txt /c/temp/lab/
 
#move hello.txt from the root directory to the ~/Desktop directory as a file called world
mv /hello.txt ~/Desktop/world
 
#copy all of the files from the code directory in fred in home to tmp
cp -R /home/fred/code /tmp/
 
#move the code directory in e:\mycode\cool stuff to tmp
mv /e/mycode/cool stuff /tmp
 
#rename hello.txt in the current directory, to goodbye.txt
mv hello.txt goodbye.txt

The rm command is used to remove (delete) files and folders. The rmdir (remove directory) command can also be used for deleting directories.

Warning: There is no undelete option when you use rm. The files are not sent to the Recycle Bin or similar location. When you delete a file or a directory with rm it is gone. Please take care with this command.
#delete the hello.txt file from the Desktop folder
rm ~/Desktop/hello.txt
 
#delete ALL the files in the tmp/blah directory - no prompts... no output
rm -rf /tmp/blah/*
 
#delete the homework folder from your home directory, and all of the files and directories within it
rmdir -rf ~/homework
 
#delete the /tmp/blah directory and all its contents... showing the files deleted, but without prompting
rm -rfv /tmp/blah
 
#delete Windows... really... try it :)
rm -rf /c/Windows

You can also create directories on the command line using the mkdir command (make directory).

#Make a src directory in your home directory
mkdir ~/src
 
#Make a cool code directory in the current directory ('\ ' = a space)
mkdir cool code
 
#Try to recreate the Windows directory you deleted... wont help... maybe install Linux :)
mkdir /c/Windows

Change Permissions

Unix file systems have three permissions on each file and directory: read, write and execute. These permissions can be changed for the user who owns the files, for the group that the file is located in, and for other users on the system. The chmod command allows you to change these permissions. For example you may want to make a file readable by all users (the user, group, and others), or you may want to make a script you downloaded executable for the user who downloaded it.

With files these permissions allow you to do the following.

  • The read permission allows you to read the contents of a file.
  • The write permission allows you to change the contents of the file.
  • The execute permission allows you to run (execute) the file.

With directories these permissions allow you to do the following.

  • The read permission allows you to read the names of the files in the directory.
  • The write permission allows you to add new files to the directory.
  • The execute permission allows you access the file in the directory.

The chmod command allows you to change these permissions (modifiers) using the format chmod [who] [+/-] [what].

Who can be one or more of:

  • u for the user who owns the file
  • g for the group associated with the file
  • o for other users
  • a for all users (the same as ugo

What can be one or more of:

  • r change the read permissions
  • w change the write permissions
  • x change the execute permissions

The + allow you to add this permission, where are - allows you to remove (or revoke) that permission.

You can view the permissions on the files using the ls command with the -l option.

# Make get_swingame.sh executable by all users
chmod a+x get_swingame.sh
 
# Make HelloWorld.sh executable for the user who owns the file
chmod u+x HelloWorld.sh
 
# Deny execution to the test file
chmod a-x test
 
# Make hello read and writable by the user who owns the file
chmod u+rw hello
 
# Remove read and write access to the group and others from the test file
chmod og-rw test
 
# Show the permissions on the files in the current directory
ls -l
 
# Show the permissions on the files /c/mytest directory
ls -l /c/mytest

Running Programs

In addition to the standard Bash commands, the shell can be used to execute programs. To start a program you enter the name of the program at the command line, followed by any command line arguments needed by the program. The following examples show you how to run programs at the command line by using their full names.

# Run the Free Pascal Compiler with the -arguments S2 and HelloWorld.pas 
/usr/local/bin/fpc -S2 HelloWorld.pas
 
# Run HelloWorld from the current directory
./HelloWorld
 
# Run SOS from the Release directory in the bin directory in the current directory
./bin/Release/SOS
 
# Run the gcc compiler with HelloWorld.c,-o and hello as the arguments
/usr/bin/gcc -out hello HelloWorld.c
 
# Use python to run a python script
/usr/bin/python monty.py

To save you from having to know and enter the full path of commonly run programs, the shell maintains a list of the paths to these programs in an environment variable called PATH. When you run a program at the command line the shell searches each directory in this PATH for the program. The following examples will locate these executables as they exist in directories that are in the PATH variable.

# Run the Free Pascal Compiler with the -arguments S2 and HelloWorld.pas 
fpc -S2 HelloWorld.pas
 
# Run the gcc compiler with HelloWorld.c,-o and hello as the arguments
gcc -out hello HelloWorld.c
 
# Use python to run a python script
python monty.py

Run the following script to view the contents of your PATH variable on your system.

# Get the environment and pipe it to grep to extract the lines with PATH
env | grep PATH

You can also use the which program to find out which executable is run. This can be useful when you want to find out which version of a program is running in cases where you have installed an update for example.

# Where is the fpc executable
which fpc
 
# Where is the gcc executable
which gcc
 
# Where is the ls executable
which ls

Things you want to learn

Once you have got started with Bash, and are comfortable using these commands, the following topics may be interesting to look into.

  • Exiting Bash with exit
  • Ending your session with logout
  • Finding and working with running programs with ps, kill and top
  • Matching text using grep
  • Finding matching files with find
  • Creating an empty file with touch
  • Editing files in Bash with vi
  • All the other commands... A-Z of Bash commands
  • Scripting Bash Scripting
  • Use a pipe to pass the output from one program to the input of another.
  • Text processing with sed and awk
  • To use REAL operating systems (UNIX derivative)

[edit]Links: Topic Brains
Return to Swin Brain