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

25474 questions
1338
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,955
  • 11
  • 31
  • 40
789
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
  • 4,655
  • 14
  • 46
  • 80
700
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,808
  • 10
  • 42
  • 51
684
votes
20 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
  • 26,802
  • 35
  • 91
  • 130
646
votes
4 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,359
  • 4
  • 20
  • 21
584
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
  • 14,675
  • 14
  • 50
  • 57
538
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
534
votes
7 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,489
  • 3
  • 10
  • 8
529
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
  • 16,315
  • 34
  • 73
  • 77
502
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
  • 27,777
  • 43
  • 130
  • 199
427
votes
16 answers

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

Noone should need 10 years for asking this question, like I did. If I were just starting out with Linux, I'd want to know: When to alias, when to script and when to write a function? Where aliases are concerned, I use aliases for very simple…
ixtmixilix
  • 13,040
  • 27
  • 82
  • 118
420
votes
18 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 changing more than one file. Here's my code: files=`ls -1 *.txt` for x in $files do mv $x "`basename $files…
afbr1201
  • 4,529
  • 5
  • 22
  • 18
404
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
  • 4,201
  • 2
  • 15
  • 9
391
votes
12 answers

How to view the output of a running process in another bash session?

I have left a script running on a remote machine from when I was locally working at it. I can connect over SSH to the machine as the same user and see the script running in ps. $ ps aux | grep ipcheck myuser 18386 0.0 0.0 18460 3476 pts/0 …
jwbensley
  • 7,402
  • 11
  • 35
  • 43
376
votes
13 answers

How to remove a single line from history?

I'm working in Mac OSX, so I guess I'm using bash...? Sometimes I enter something that I don't want to be remembered in the history. How do I remove it?
B Seven
  • 7,659
  • 7
  • 23
  • 20
1
2 3
99 100