There are a variety of interpreters that receive commands either interactively or as a sequence of commands from a file. The Bourne-again shell (Bash) is one such interpreter. Bash implements the standard Bourne Shell (sh), and offers numerous additions.
From the Free Software Foundation's Bash page:
Bash is an
sh-compatible shell that incorporates useful features from the KornShell (ksh) and C shell (
csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.
Read the Bash manual for technical details.
Bash was written by Brian Fox and first released in 1989. It is the default shell in many Linux distributions and on MacOS; it is available on most modern operating systems, and has been ported to Windows 10.
As of January 2019, the most recent version of
bash is 5.0, although you may be using an older version depending on your operating system and
which updates to
bash have been installed. Most Linux installations should be using something in the 4.x family. macOS (formerly Mac OS X) uses version 3.2 by default due to licensing issues.
Be sure to note in your question what version of
bash you are using. This will alert potential answerers to what features are available to you, as well as which bugs may need to be worked around.
You can determine which version of
bash you are using by running
bash --version or checking the value of the
BASH_VERSION shell variable.
Without an explicit version, an answerer may well assume you are using at least version 4.2. Questions tagged
osx imply version 3.2 unless otherwise stated.
Based on downloads available from http://ftp.gnu.org/gnu/bash/
Version Release date ======================= 3.2 2006-10-11 4.0 2009-02-20 4.1 2009-12-31 4.2 2011-02-13 4.3 2014-02-26 4.4 2016-09-15 5.0 2019-01-07
Additionally, all versions for
bash from 2.0 and later received an important patch-level release to address the Shellshock vulnerability in September 2014.
To help the kind people who assist you, to ensure that future readers can benefit from your question, and to help ensure your question is voted up as useful for that lovely karma, please make your question as simple and universal as possible:
cat -v yourfile or
echo "$yourvariable" | cat -v .
DOS carriage returns will show up as
^M after each line.
If you find them, delete them using
tr -d '\r'
The first line in the script must be
It must not be
Run the script with
Do not run it with
This applies even when
sh is a symlink to
shellcheckor the online shellcheck service to automatically check for common mistakes.
cat -v yourfileor
cat -v <<< "$yourvar". Be especially careful with carriage returns (shown as
bashtag should be reserved for Bash-related problems, not any CLI problem you might have.
For example, let's say you have a script for alerting you when a server is idle, but it keeps alerting even when the machine is not idle:
# Avoid code like this when asking about a problem # It has irrelevant code and external dependencies, and is hard to read and run while true do load=$(wget -O - "http://$1/load.php" | grep "^load:" | cut -d: -f 2) if [[ $load=="0" ]] then mailx -s "System is idle" firstname.lastname@example.org <<< "The server is idle" break else echo "Waiting..." sleep 60 fi done
echo "Why does this run?"
We're now left with this small, self-contained example:
# Prefer code like this when asking about a problem # It's small, simple and self contained, making it easy to read and run. load=42 if [[ $load=="0" ]] then echo "Why does this run?" fi
Thanks for making your question simple and useful! Enjoy your upvotes!
(However, note that this example is simple to compare against the relevant entry in Bash pitfalls and the error is automatically caught by
shellcheck, so now you don't actually need to ask!)
Some frequently asked Bash questions include the following.
Some fundamentals of Bash are surprising even to veterans from other programming languages.
command not foundmessage
=in variable assignment
=in an assignment?
[ -f many things ]doesn't work
[is part of the
ifstatement's syntax but it isn't
help testto see them.
awka bunch of pairs of files
if [[ string =~ $regex ]]
These questions are not really specific to Bash, but frequent enough in this tag that they deserve to be included here.
crontabtag wiki also has a good set of notes for troubleshooting
Additional reading materials include:
Bash FAQ by Lhunath
Bash Guide by Lhunath
The Command Line Crash Course (also a Powershell reference)
shellcheck- a static analysis tool that detects common mistakes
The Stack Overflow
bash chat is useful for coordinating work within this tag, and perhaps occasionally for getting quick help (though no guarantees can be made; attendance is spotty).