Questions tagged [bash]

This tag is for questions about scripts written for the Bash command shell. For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here. Questions about the interactive use of Bash are more likely to be on-topic on Unix & Linux Stack Exchange or Super User than on Stack Overflow.

About Bash

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; it is available on most modern operating systems, and has been ported to Windows 10.

A note regarding versions

As of September 2022, the most recent version of bash is 5.2, 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) only provides version 3.2 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 (it's been available for over 10 years). Questions tagged imply version 3.2 unless otherwise stated.

A Brief Release History

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
5.1 2020-12-06
5.2 2022-09-26

Additionally, all versions for bash from 2.0 and later received an important patch-level release to address the Shellshock vulnerability in September 2014.

Before asking about problematic code

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:

  1. Check whether your script or data has DOS style end-of-line characters

    • Use 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 dos2unix (a.k.a. fromdos) or tr -d '\r'

  2. Make sure you run the script with bash, not sh

    • The first line in the script must be #!/bin/bash or #!/usr/bin/env bash.

      It must not be #!/bin/sh even if your system's /bin/sh is a symlink to /bin/bash

    • Run the script with ./yourscript or bash yourscript.

      Do not run it with sh yourscript.

      This applies even when sh is a symlink to bash.

  3. Find a small, self-contained example.

    • Don't include sections and commands unrelated to your problem.
    • Avoid complex commands that just serve to produce a value (include the value directly).
    • Avoid relying on external files. Create the files on the fly, include the data directly, or post a small example of a file in your question.
  4. Test your example. Make sure it runs and still shows the problem. Do not brush this off.

    • Reformatting for clarity often sidesteps pitfalls related to spacing and naming.
    • Refactoring for simplicity often sidesteps pitfalls related to subshells.
    • Mocking out files and data often sidesteps problems related to special characters.
    • Hours spent trying multiple things often leads to posting code from one version and errors from another.
  5. Check the example for common problems

    • Run your example through shellcheck or the online ShellCheck service to automatically check for common mistakes.
    • Browse Bash pitfalls and Bash beginner's mistakes as well as the Popular Questions section below for checklists of common issues.
    • Check your data for special characters, using cat -v yourfile or cat -v <<< "$yourvar". Be especially careful with carriage returns (shown as ^M).
  6. Please avoid tagging questions that are solely about external commands. The bash tag should be reserved for Bash-related problems, not any CLI problem you might have.

How to turn a bad script into a good question

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" user@example.com <<< "The server is idle"
    break
  else
    echo "Waiting..."
    sleep 60
  fi
done
  1. The problem still occurs without the loop: Remove the loop from your question.
  2. The problem still occurs if you skip asking the server: Hard code the response (e.g. load=42)
  3. The problem still occurs without emailing: Use echo "Why does this run?"
  4. The problem still occurs when removing the else branch. Shorten it

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!)

Popular Questions

Some frequently asked Bash questions include the following.

Basic Syntax and Common Newbie Problems

Some fundamentals of Bash are surprising even to veterans from other programming languages.

How Do I ...?

Why Does ...?

Common Tasks

These questions are not really specific to Bash, but frequent enough in this tag that they deserve to be included here.

Meta

Books and Resources

Additional reading materials include:

Tools

  • shellcheck - a static analysis tool that detects common mistakes
  • on-line ShellCheck, a web server providing shellcheck (useful if you've not yet installed the program)
  • https://explainshell.com/ can pick apart many command lines and explain what the elements mean (notice that you can sometimes click on a result to have it picked apart further)

Chat

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).

154003 questions
74
votes
7 answers

Using the result of a command as an argument in bash?

To create a playlist for all of the music in a folder, I am using the following command in bash: ls > list.txt I would like to use the result of the pwd command for the name of the playlist. Something like: ls > ${pwd}.txt That doesn't work though…
HoboBen
  • 3,421
  • 2
  • 26
  • 22
74
votes
11 answers

bash: silently kill background function process

shell gurus, I have a bash shell script, in which I launch a background function, say foo(), to display a progress bar for a boring and long command: foo() { while [ 1 ] do #massively cool progress bar display code sleep 1 …
rouble
  • 16,364
  • 16
  • 107
  • 102
74
votes
7 answers

Using command line argument range in bash for loop prints brackets containing the arguments

It's probably a lame question. But I am getting 3 arguments from command line [ bash script ]. Then I am trying to use these in a for loop. for i in {$1..$2} do action1 done This doesn't seem to work though and if $1 is "0" and $2 is 2 it…
ru4mqart668op514
  • 743
  • 1
  • 5
  • 5
74
votes
4 answers

"Couldn't find a file descriptor referring to the console" on Ubuntu bash on Windows

I have a problem with Bash on Ubuntu on Windows. If I type "open (filename)" on Mac terminal, it opens the file with the right program but if I try to use it on Windows bash, it says: "Couldn't find a file descriptor referring to the console". I…
Pets
  • 741
  • 1
  • 5
  • 3
74
votes
2 answers

jq not working on tag name with dashes and numbers

I am using jq but having "-" in my json tag make jq not compile. I cannot escape it to make it works. Here is the command: curl -X GET -H "X-AppKey:foo" "foo/v2/_status" | jq '.component-status[]' I have read in the github of jq this post…
paul
  • 12,873
  • 23
  • 91
  • 153
74
votes
5 answers

How can I suppress error messages of a command?

How can I suppress error messages for a shell command? For example, if there are only jpg files in a directory, running ls *.zip gives an error message: $ ls *.zip ls: cannot access '*.zip': No such file or directory Is there an option to…
Peter
  • 1,224
  • 3
  • 16
  • 28
74
votes
10 answers

How to write stdout to file with colors?

A lot of times (not always) the stdout is displayed in colors. Normally I keep every output log in a different file too. Naturally in the file, the colors are not displayed anymore. I'd like to know if there's a way (in Linux) to write the output…
AAlvz
  • 1,059
  • 2
  • 9
  • 15
74
votes
1 answer

Store grep output in an array

I need to search a pattern in a directory and save the names of the files which contain it in an array. Searching for pattern: grep -HR "pattern" . | cut -d: -f1 This prints me all filenames that contain "pattern". If I try: targets=$(grep -HR…
Luca Davanzo
  • 21,000
  • 15
  • 120
  • 146
74
votes
5 answers

Get UTC time in seconds

It looks like I can't manage to get the bash UTC date in second. I'm in Sydney so + 10hours UTC time date Thu Jul 3 17:28:19 WST 2014 date -u Thu Jul 3 07:28:20 UTC 2014 But when I tried to convert it, I'm getting the same result which is not…
Romanzo Criminale
  • 1,289
  • 2
  • 14
  • 21
74
votes
4 answers

Recursively cat all the files into single file

I have bunch of files sitting in folders like data\A\A\A\json1.json data\A\A\A\json2.json data\A\A\B\json1.json ... data\Z\Z\Z\json_x.json I want to cat all the jsons into one single file?
frazman
  • 32,081
  • 75
  • 184
  • 269
74
votes
4 answers

How to set an alias inside a bash shell script so that is it visible from the outside?

OSX: This works from the command line: alias ruby="/opt/local/bin/ruby1.9" but in side a shell script, it has no effect. I want to write a script that will switch between ruby 1.8 and ruby 1.9, so this needs to be a script - not in my profile. It…
phil swenson
  • 8,564
  • 20
  • 74
  • 99
74
votes
5 answers

Concatenate in bash the output of two commands without newline character

What I need: Suppose I have two commands, A and B, each of which returns a single-line string (i.e., a string with no newline character, except possibly 1 at the very end). I need a command (or sequence of piped commands) C that concatenates the…
synaptik
  • 8,971
  • 16
  • 71
  • 98
74
votes
7 answers

Append line to /etc/hosts file with shell script

I have a new Ubuntu 12.04 VPS. I am trying to write a setup script that completes an entire LAMP installation. Where I am having trouble is appending a line to the /etc/hosts file. My current hosts file looks like this: 127.0.0.1 localhost…
Stephen Howells
  • 909
  • 1
  • 7
  • 11
74
votes
7 answers

Shell - check if a git tag exists in an if/else statement

I am creating a deploy script for a zend application. The script is almost done only I want to verify that a tag exists within the repo to force tags on the team. Currently I have the following code: # First update the repo to make sure all the tags…
Kim
  • 977
  • 1
  • 8
  • 15
74
votes
3 answers

How can I execute a group of commands as another user in Bash?

There are already some existing questions asked here about running commands as another user. However, the question and answers focus on a single command instead of a long group of commands. For example, consider the following…
Nathan Osman
  • 71,149
  • 71
  • 256
  • 361