Questions tagged [bash]

Bash (Bourne-Again SHell), is the default interactive user shell used in Ubuntu. Use this tag for questions specific to Bash, as opposed to generic POSIX shells.

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 csh and ksh. 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
  • 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 sh (POSIX rather than Bash specific).
  • Use if you have a question about writing shell scripts (either POSIX or Bash specific).
11217 questions
835
votes
9 answers

How to increment a variable in bash?

I have tried to increment a numeric variable using both var=$var+1 and var=($var+1) without success. The variable is a number, though bash appears to be reading it as a string. Bash version 4.2.45(1)-release (x86_64-pc-linux-gnu) on Ubuntu 13.10.
user221744
  • 8,361
  • 3
  • 13
  • 4
645
votes
8 answers

How do I add environment variables?

I'm running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable: $r@hajt:~$ env THEVAR=/example But it's not working. It shows all the variables with THEVAR being the last one, but another call to env…
huff
  • 6,550
241
votes
4 answers

What does an "exec" command do?

I have seen exec command used inside scripts to redirect all output to a file (as seen in this). But in simple words, what does it do?
a06e
  • 13,223
  • 26
  • 70
  • 104
173
votes
11 answers

How to pass an array as function argument?

Struggling for a while passing an array as argument but it's not working anyway. I've tried like below: #! /bin/bash function copyFiles{ arr="$1" for i in "${arr[@]}"; do echo "$i" done } array=("one" "two"…
Ahasanul Haque
  • 1,843
  • 2
  • 11
  • 6
164
votes
9 answers

Hide current working directory in terminal

As a default, the terminal prompt shows something like: user@system:/folder1/folder2/folder3/folder4$ the deeper you go into the filesystem, the less space is left for typing in commands. Can I change this to only show something like: > I mean, I…
Theodor
  • 1,735
120
votes
2 answers

Reload bash's .profile without logging out and back in again

Is it possible to coerce bash to reload the .profile file without logging out and back in again?
Matty
  • 2,384
113
votes
4 answers

How to access the last return value in bash?

Simple scenario: I'm looking for a wsdl file lost in the middle of a project. $ find -name '*.wsdl' ./some/very/very/long/way/to/some/lost/directory/filename.wsdl Now that I know where it is, I want to do something with this file, say edit it. …
Anto
  • 2,661
106
votes
12 answers

Adding timestamps to terminal prompts?

I was wondering if it was possible to add timestamps to terminal prompts, and if so, how could I achieve this?
Jan Gorzny
  • 1,163
96
votes
8 answers

How to reinitialize a terminal window instead of closing it and starting a new one?

When I make some changes to the shell/bash behavior, such as setting up an alias, is there a quick command to reinitialize the terminal window instead of closing and opening a new window?
NES
  • 33,195
78
votes
7 answers

How to delete selected results from bash history?

history command shows all the results but we can filter to get particular command using history | grep searchingCommand. It is really helpful. But the problem is it shows those commands also which was entered with typo error or which was…
Saurav Kumar
  • 14,916
73
votes
4 answers

What does $# mean in bash?

I have a script in a file named instance: echo "hello world" echo ${1} And when I run this script using: ./instance solfish I get this output: hello world solfish But when I run: echo $# It says "0". Why? I don't understand what $#…
solfish
  • 1,531
65
votes
5 answers

Where is .bash_profile?

I want to put new aliases to my .bash_profile file, but I cannot find this file. Where is it supposed to be?
Shira
  • 899
64
votes
6 answers

Best way to read a config file in bash

What is the best way to read a config file in bash? For example, you have a script and aren't willing to fill in all the config manually each time you call the script. Edit 1: I think I didn't make it clear, so: what I want is... I have a…
IcyIcyIce
  • 1,119
55
votes
1 answer

Pause execution and wait for user input

I have a script that I am making and I have an issue with it: I would like to pause execution and wait for user input. I thought that I had it with the read -p -n 1 $foo command but the system is having issues with this command. Here is my current…
Elliot Huffman
  • 797
  • 1
  • 5
  • 16
51
votes
5 answers

How can I set a single .bashrc file for several users?

during my work I need to constantly add alias commands to bashrc, most of those commands needs to be runed by other users. is there any way I could add alias commands to a bashrc from external source?
1
2 3
48 49