Bash
The Bourne Again Shell, or Bash, is a Unix-based shell program and language used for a multitude of purposes including system administration and software testing.
Developed by Brian Fox in 1989, Bash was released as part of the GNU Project to replace the original Bourne Shell. It was also one of the first programs ported to Linux.
Operating System Compatibility
Over time, Bash has become available across many operating systems both as a default shell or an installable program.
Linux
Due to their mutual relationship with the GNU Project, Bash is the default shell on most distributions of Linux such as the following:
macOS
Apple macOS featured Bash as the default from 2003 with OS X Panther (version 10.3), to 2019 with Catalina (version 10.15). Since then, Z shell (or zsh
) is the default shell for macOS.
Note: Bash can still be used as an alternative in newer versions of macOS. The switch can be made by:
- Running
chsh -s bin/bash
in the Terminal window.- Confirming the change with the user’s credentials.
- Closing and reopening the Terminal window.
Windows
Bash is not the default shell for Windows operating systems, but it can be enabled through the Windows Subsystem for Linux (WSL). This runs a Linux environment without the need for a virtual machine.
Bash Terminal Commands
The basic terminal commands for navigating and manipulating directories and files are as follows -
cat
Command
The cat
(short for concatenate) command is used to display the contents of a file in the terminal. It can also be used to concatenate multiple files into one. By default, it outputs the contents of the file(s) to the terminal.
# Display the contents of a filecat filename.txt# Display the contents of multiple filescat file1.txt file2.txt# You can also use redirection to concatenate files into a new filecat file1.txt file2.txt > combined.txt
Navigation Commands
In Bash, navigation commands are used to move through directories and the file system efficiently.
Print Working Directory Command
Use the pwd
command to display the current directory -
pwd
List Command
Use the ls
command to list the contents of the current directory -
# To list the contents of the current directoryls# Use the -l argument to get a detailed list with more informationls -l
Note: If
ls
is used with the-a
argument, it also shows hidden files.
Change Directory
The cd
command is used to change directories -
# To go to a specific directorycd directoryPath# To go to the Previous Directorycd -# To go up one directory levelcd ..
Directory Management
In Bash, directory management involves creating, deleting, moving, copying, and manipulating directories within the file system.
Make a New Directory
To create a new directory, use the mkdir
command followed by the new directory name -
mkdir newDirectoryName
Remove a directory
To delete a directory, use the rmdir
command with the directory’s name -
rmdir directoryName
File Management
In Bash, file management involves creating, deleting, moving, copying, and manipulating files within the file system.
Create a new file
To create a new empty file, use the touch
command followed by the file name -
touch filename.txt
Remove a file
To delete a file, use the rm
command with the file name or file path -
rm filename.txt
Rename or move a file
To rename a file or move it to a new location, use the mv
command -
# Rename the file (original name is the first argument, new name is the second)mv oldFileName.txt newFileName.txt# Move a file to a different directory (source path is the first argument, destination path is the second)mv sourceDirectory/filename.txt destinationDirectory/filename.txt
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Command Line on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn the Command Line
Learn about the command line, starting with navigating and manipulating the file system, and ending with redirection and configuring the environment.With CertificateBeginner Friendly4 hours