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

25958 questions
815
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,035
  • 15
  • 50
  • 84
329
votes
3 answers

How can I make "Press any key to continue"

I'm making a script to install my theme, after it finished installing it will appear the changelog and there will be "Press any key to continue" so that after users read the changelog then press any key to continue
215
votes
11 answers

How to export variables from a file?

I have a tmp.txt file containing variables to be exported, for example: a=123 b="hello world" c="one more variable" How can I export all these variables using the export command, so that they can later be used by child processes?
Neerav
  • 3,095
176
votes
8 answers

Argument string to integer in bash

Trying to figure out how to convert an argument to an integer to perform arithmetic on, and then print it out, say for addOne.sh: echo $1 + 1 >>sh addOne.sh 1 prints 1 + 1
user135986
  • 1,769
  • 2
  • 11
  • 3
119
votes
13 answers

Kill all background jobs

Is there a more compact form of killing background jobs than: for i in {1..5}; do kill %$i; done Also, {1..5} obviously has a hard-coded magic number in it, how can I make it "N" with N being the right number, without doing a: $(jobs | wc -l) I…
74
votes
6 answers

Difference between printf and echo in bash

What is the difference between the printf function in bash and the echo function? Specifically, running: echo "blah" >/dev/udp/localhost/8125 did not send the "blah" command to the server listening on 8125, whereas printf "blah"…
Kevin Burke
  • 1,971
63
votes
5 answers

Launch a background process and check when it ends

How can I launch a process in background and check when it ends within a bash script? My idea is a script like this: launch backgroundprocess & while [ Process is running ];do echo "PROCESS IS RUNNING\r" done; echo "PROCESS TERMINATED"
0wn3r
  • 731
63
votes
2 answers

Modify global variable in while loop

I have a script that process a folder, and count the files in the mean time. i=1 find tmp -type f | while read x do i=$(($i + 1)) echo $i done echo $i However, $i is always 1, how do I resolve this?
daisy
  • 54,555
61
votes
2 answers

Why does bash think: 016 + 1 = 15?

Can somebody explain to me why a number with a leading 0 gives this funny behaviour? #!/bin/bash NUM=016 SUM=$((NUM + 1)) echo "$NUM + 1 = $SUM" Will print: 016 + 1 = 15
DeltaLima
  • 713
  • 5
  • 8
58
votes
7 answers

Setting IFS for a single statement

I know that a custom IFS value can be set for the scope of a single command/built-in. Is there a way to set a custom IFS value for a single statement?? Apparently not, since based on the below the global IFS value is affected when this is…
iruvar
  • 16,725
58
votes
11 answers

Displaying seconds as days/hours/mins/seconds?

Is it possible to easily format seconds as a human-readable time in bash? I don't want to format it as a date, but as the number of days/hours/minutes, etc...
Tyilo
  • 5,981
56
votes
6 answers

How to find the file where a bash function is defined?

I cannot figure out how to find the file where a bash function is defined (__git_ps1 in my case). I experimented with declare, type, which, but nothing tells me the source file. I read somewhere that declare can print the file name and the line…
Alexey
  • 1,988
55
votes
4 answers

Get the free space available in current directory in Bash

I know df -h and pwd, but it seems a little complex for the regex matching part. Any ideas?
Cheng
  • 6,641
54
votes
8 answers

How to keep Bash running after command execution?

I would like to run something like this: bash -c "some_program with its arguments" but to have an interactive bash keep running after some_program ends. I'm sure -c is not a good way as man bash seys: An interactive shell is one started without…
pawel7318
  • 2,000
  • 3
  • 16
  • 15
52
votes
5 answers

Can bash write to its own input stream?

Is it possible in an interactive bash shell to enter a command that outputs some text so that it appears at the next command prompt, as if the user had typed in that text at that prompt ? I want to be able to source a script that will generate a…
starfry
  • 7,442
1
2 3
99 100