Bash/Command Line

Welcome to the NeuroNest tutorial on Bash and Command Line! This tutorial will show the basics of Bash scripting, allowing beginners to master the concepts of the Bash shell on Linux.

1. Introduction to Bash

What is Bash?

The name Bash stands for “Bourne Again SHell,” a reference to its predecessor, the Bourne shell (sh), written by Stephen Bourne. Bash is a commonly-used command-line interface (CLI) and scripting language that allows you to efficiently execute commands, manage files, and automate tasks in Unix-like systems. A command line interface (CLI) is a text-based interface where you can input commands that interact with a computer's operating system. The command line or terminal is where you write Bash commands that the computer then executes.

What is a shell?

A “shell” is a program that provides a way for users to interact with the computer's operating system. Specifically, a shell's primary purpose is to read commands and run other programs. Shells can be a command-line interface (CLI) or a graphical user interface (GUI). The CLI allows users to type commands to run processes and manage the system, while the GUI provides a more visual approach. Bash is one of the most commonly used Unix/Linux shells.

What is Unix?

Unix is a powerful operating system known for its multitasking and multiuser capabilities. It provides a command-line interface and a scripting language, which allows users to automate tasks and manage complex workflows efficiently. Bash is often the default shell in many Unix-like systems, including Linux and MacOS, and is also available on Windows through tools like Git for Windows.

This tutorial will guide you through basic and advanced commands to help you get comfortable using the command line. For more information and examples, visit our extended command line page.

2. Accessing the Command Line

To follow along with this tutorial, you will need a running version of Linux with access to the command line. If you do not already have the shell software installed, you will need to download and install it.
  • macOS: The shell application is called “Terminal,” located in Applications > Utilities, or you can search for "Terminal" using Spotlight.

  • Linux: Terminal applications are usually pre-installed. You can open it from the application menu or by pressing Ctrl + Alt + T.

  • Windows: Install git for Windows to get Git Bash or use Windows Subsystem for Linux (WSL) for a more comprehensive Unix-like environment.

For troubleshooting and installation assistance, refer to these resources:

3. Getting Started

To get started, open the command line. You should see a prompt, which typically ends in a $, but can also end with #, >, %, or >>>. The prompt indicates that the shell is ready for input. This prompt is also called the “command line”. It should look something like this:

$

Your prompt might look different. Most shell environments put your user name and the host name before the prompt symbol. For example, your prompt might look like:

amordy@localhost $

The prompt is followed by a text cursor, a character (usually a flashing or solid block) that indicates the position where your typing will appear. You can type commands into this prompt and press the enter or return key to execute them. The commands entered at the command line are sent to the computer’s operating system to do a variety of different operations, or to launch various other programs. In some of the example code, you will see file and directory names in brackets (e.g., [username]). Text in brackets should be replaced with your own names.

Do NOT type the prompt when typing commands. Only type the command that follows the prompt.

Here are some helpful tips for navigating the command window: keyboard shortcuts

4. File Structure

Your computer organizes data in files and folders. Files can be placed in folders, which we call directories. Basically, a directory is a location in the file system that can contain files and other directories (subdirectories), creating a 'directory tree'. It's essentially a folder where files and other folders are organized.

At the top of the directory tree is the root directory", which contains all of our directories. We refer to the root directory using the forward slash /. Inside the root directory are several other directories, such as bin (stores built-in programs) and Users (stores each users' personal directories and files).

tree

While we are using the shell, we can only be in one directory at a time (unless multiple windows are open). This is called our current working directory. When you first start the shell, your current working directory will most likely be your home directory. This means that the files and folders in your home directory are immediately accessible to you.

A path is a string that specifies the location of a file or directory in the file system (e.g., ~/Users/[name]/Downloads)

  • Absolute Path: Specifies the full path from the root directory. Example: $ /Users/name/Desktop/subdirectory

  • Relative Path: Specifies a path relative to the current working directory. Example: $ Desktop/subdirectory

  • Home Directory: The tilde ~ represents the home directory.

  • Current Directory: . refers to the current working directory.

  • Parent Directory: .. refers to the parent directory.

Most home directories follow this path format: /Users/[name]. In this example, we know that the directory [name] is stored within the directory /Users because Users is in the first part of the path. In addition, we know that /Users is within the root directory (/) because its name begins with /

Commands are executed through the command line. To execute a command, type it into the command line after the prompt and press return or enter.

To see your current working directory, we will execute the command that will print the working directory, pwd.

$ pwd 

Directories and absolute paths (i.e. exact position in the system) are always prefixed with a /.

Next, to see the files within our curent working directory, we will execute the command that prints a list of the files and directories within our working directory, ls. Your output is a list of the files and directories within your current working directory.

$ ls

5. Common Commands

Command Description
Navigating Directories
pwd Print working directory path.
ls List files and directories in the current directory.
cd Change directory.
cd ~ Change directory to home directory.
cd .. Move up one level in the directory tree.
mkdir Make directory.
rmdir Remove directory (only if empty)
Working with Files
cp Copy files or directories
rm Remove files or directories
mv Move or rename files or directories
nano Opens the specified file in the nano text editor.
touch Creates an empty text file.
grep Search text using patterns
find Search for files in a directory hierarchy
echo Display a line of text
cat Display file
less Display a few lines of file at a time
head Display first 10 lines of file.
tail Display last 10 lines of file.
echo $NAME Prints the value of the variable "NAME".
Command Help
man command Opens the manual for "command".
command --help Displays help text for "command".
Managing Workspace
clear Clears the terminal screen, keeping command history.
exit Exits the terminal.

For additional information on commands, visit our extended tutorial on command line.

6. Unix Operating System Capabilities

Bash Comnand Line and Scripting Tutorial

Please visit for a step-by-step tutorial with an fMRI dataset i

Shell Scripting

Shell scripting is a very useful tool for automating tasks. To learn more, visit the Shell Scripting page.

Permissions

To learn more about file/directory permissions and how to change them, please visit the Permissions page.

Secure Shell

Please visit the Secure Shell page for more information.

Version Control with Git and GitHub

Bash can be used to access and interact with Git (version control). To get started with Git, GitHub, and version control, see our Github & Doctor page and explore this resource on version control.

Working with Virtual Environments and Containers

7. Suggested Tutorials

For an in-depth tutorial on how to use the command shell, visit Software Carpentry's "The Unix Shell".

Another great resource is Andy's Brain Book Unix for Neuroimagers.

Here is a tutorial with Software Carpentry's Extra Unix Shell Material.