Questions tagged [bash]

Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead. For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here.

Bash (the GNU Bourne Again SHell) is a Unix shell. It was built as a free replacement to the Bourne shell and includes many scripting features from other shells, such as ksh and (t)csh. When called as sh, it is intended to conform to the POSIX 1003.1 standard. Bash features include: command line editing with the readline library, command history, job control, functions & aliases, arrays, dynamic prompts, integer arithmetic, and command & filename completion. Bash is the default interactive shell on most Linux distributions and is usually available on other Unix variants. Some GNU/Linux systems even use it as the default shell /bin/sh.

Because Bash is a common shell, you may be using it by default, so beware the temptation to choose this tag by default! Use only if your question is about Bash-specific syntax or the interactive use of Bash. Use the tag instead if your question is about a sh (Bourne or POSIX) script. Use if you have a question about a shell’s interaction with other programs.

Before asking for help about problems with Bash scripts, consider debugging the script yourself first.

Related tags

  • Many shell-agnostic questions are of interest to Bash users.
  • For questions about shell scripting in general

Other shells

  • - the Korn shell
  • - the C shell
  • - the TENEX C shell
  • - the Z shell
  • - the Debian Almquist shell
  • - the friendly interactive shell

Features related to Bash

  • (or globbing): matching files based on their name
  • a history of commands that can be navigated with the Up and Down keys, searched, etc.; also a recall mechanism based on expanding sequences beginning with !
  • completion of partially-entered file names, command names, options and other arguments
  • showing a prompt before each command, which many users like to customize
  • the GNU library implementing the line editing and history handling in Bash (and other terminal applications like gdb and python)
  • for defining shortcuts for frequently-used commands
  • a data structure for storing items in index-able memory

Bash reference material

Sampling of Bash-related Unix.SE questions:

Books and other resources

24056 questions
1274
votes
12 answers

How to correctly add a path to PATH?

I'm wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it's not clear how to do this. This way: export PATH=~/opt/bin:$PATH or this? export…
Paolo
  • 16,105
  • 10
  • 29
  • 40
734
votes
4 answers

How to cycle through reverse-i-search in BASH?

In the terminal, I can type Ctrl + R to search for a matching command previously typed in BASH. E.g., if I type Ctrl + R then grep, it lists my last grep command, and I can hit enter to use it. This only gives one suggestion though. Is there any way…
Village
  • 5,299
  • 14
  • 45
  • 75
680
votes
24 answers

Preserve bash history in multiple terminal windows

I consistently have more than one terminal open. Anywhere from two to ten, doing various bits and bobs. Now let's say I restart and open up another set of terminals. Some remember certain things, some forget. I want a history that: Remembers…
Oli
  • 15,218
  • 7
  • 41
  • 51
633
votes
3 answers

Using "${a:-b}" for variable assignment in scripts

I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation VARIABLE1="${VARIABLE1:-some_val}" or some expand other variables VARIABLE2="${VARIABLE2:-`echo…
Justin Garrison
  • 7,207
  • 3
  • 19
  • 21
625
votes
19 answers

How to get execution time of a script effectively?

I would like to display the completion time of a script. What I currently do is - #!/bin/bash date ## echo the date at start # the script contents date ## echo the date at end This just show's the time of start and end of the script. Would it be…
mtk
  • 25,150
  • 34
  • 89
  • 125
555
votes
27 answers

How can I resolve a hostname to an IP address in a Bash script?

What's the most concise way to resolve a hostname to an IP address in a Bash script? I'm using Arch Linux.
Eugene Yarmash
  • 13,945
  • 12
  • 43
  • 56
505
votes
10 answers

How to conditionally do something if a command succeeded or failed

How can I do something like this in bash? if "`command` returns any error"; then echo "Returned an error" else echo "Proceed..." fi
Shinmaru
499
votes
6 answers

What is the difference between the Bash operators [[ vs [ vs ( vs ((?

I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis). [[ , [ , ( , (( I have seen people use them on if statements like this : if [[condition]] if…
RetroCode
  • 5,139
  • 3
  • 9
  • 7
461
votes
14 answers

How do I loop through only directories in bash?

I have a folder with some directories and some files (some are hidden, beginning with dot). for d in *; do echo $d done will loop through all files and directories, but I want to loop only through directories. How do I do that?
rubo77
  • 25,579
  • 39
  • 121
  • 184
444
votes
4 answers

In a bash script, using the conditional "or" in an "if" statement

This question is a sequel of sorts to my earlier question. The users on this site kindly helped me determine how to write a bash for loop that iterates over string values. For example, suppose that a loop control variable fname iterates over the…
Andrew
  • 14,895
  • 34
  • 72
  • 77
422
votes
16 answers

In Bash, when to alias, when to script, and when to write a function?

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just starting out with Linux, I'd want to know: When to…
ixtmixilix
  • 12,960
  • 26
  • 79
  • 110
381
votes
13 answers

How can I get the size of a file in a bash script?

How can I get the size of a file in a bash script? How do I assign this to a bash variable so I can use it later?
haunted85
  • 3,971
  • 2
  • 14
  • 9
365
votes
17 answers

How do I change the extension of multiple files?

I would like to change a file extension from *.txt to *.text. I tried using the basename command, but I'm having trouble on changing more than one file. Here's my code: files=`ls -1 *.txt` for x in $files do mv $x "`basename $files…
afbr1201
  • 3,969
  • 5
  • 21
  • 18
360
votes
6 answers

Why does my shell script choke on whitespace or other special characters?

Or, an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some file names). I encountered a problem such as the…
Gilles 'SO- stop being evil'
  • 766,554
  • 187
  • 1,586
  • 2,083
350
votes
4 answers

How do I clear Bash's cache of paths to executables?

When I execute a program without specifying the full path to the executable, and Bash must search the directories in $PATH to find the binary, it seems that Bash remembers the path in some sort of cache. For example, I installed a build of…
Daniel Trebbien
  • 3,725
  • 2
  • 14
  • 9
1
2 3
99 100