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).
11385 questions
978
votes
15 answers

How to get bash or ssh into a running container in background mode?

I want to ssh or bash into a running docker container. Please, see example: $ sudo docker run -d webserver webserver is clean image from ubuntu:14.04 $ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS …
885
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,861
  • 3
  • 14
  • 4
711
votes
8 answers

How can I decode a base64 string from the command line?

I would like to write a bash script to decode a base64 string. For example I type decode QWxhZGRpbjpvcGVuIHNlc2FtZQ== and it prints Aladdin:open sesame and returns to the prompt. So far I have tried a simple bash file containing python -m base64 -d…
lofidevops
  • 21,912
674
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,840
627
votes
7 answers

Which one is better: using ; or && to execute multiple commands in one line?

In tutorials and how-to's I often see commands combined. For instance, sudo apt-get update && sudo apt-get install pyrenamer There seem to be four possible connectors: &, &&, || and ;. Though the & connector is clear to me (it sends a process to…
don.joey
  • 29,392
591
votes
3 answers

How do I use variables in a sed command?

I tried the following code to replace QQ with ZZ, but it doesn't do what I want: var1=QQ sed -i 's/$var1/ZZ/g' $file However, this code does what I want: sed -i 's/QQ/ZZ/g' $file How do I use variables in sed?
UAdapter
  • 17,967
580
votes
6 answers

How to see the command attached to a bash alias?

Suppose I have an alias in the bash shell. Is there a simple command to print out what command the alias will run?
Casebash
  • 6,059
562
votes
10 answers

How do I create a permanent Bash alias?

I would like to create an alias to rm command in order to have a confirmation message after executing this command. So I am creating an alias like this alias rm='rm -i'. But as far as I know this is a temporary alias and it lives until you close the…
Bakhtiyor
  • 12,804
556
votes
11 answers

How to list all variables names and their current values?

How to list all variables names and their current values? Including not only $HOME, $PWD etc but any other you have defined.
Strapakowsky
  • 12,304
529
votes
17 answers

How do I run .sh scripts?

Whenever I open a .sh file, it opens it in gedit instead of the terminal. I can't find any option similar to Right Click → Open With → Other Application... → Terminal. How do I open this file in the terminal?
Alex
  • 5,291
425
votes
4 answers

What is the difference between #!/bin/sh and #!/bin/bash?

if I write, #!/bin/bash echo "foo" or #!/bin/sh echo "foo" both yields same. I have seen some scripts starting with #!/bin/sh or #!/bin/bash. Is there any difference between them?
Rahul Virpara
  • 11,860
412
votes
5 answers

What does 2>/dev/null mean?

I would like a brief explanation of the following command line: grep -i 'abc' content 2>/dev/null
Naive
  • 4,895
  • 11
  • 28
  • 35
408
votes
9 answers

How to see time stamps in bash history?

Is there any way I can see at what time the commands were executed from the bash history? We can see the order but is there any way I can get the time also? Bottom-Line: Execution time in the Bash history
Raja G
  • 105,327
  • 107
  • 262
  • 331
395
votes
10 answers

Run a shell script as another user that has no password

I would like to run a script from the main ubuntu shell as a different user that has no password. I have full sudo privileges, so I tried this: sudo su -c "Your command right here" -s /bin/sh otheruser Then I have to enter my password, but I am not…
rubo77
  • 34,024
  • 52
  • 172
  • 299
392
votes
9 answers

How to clear bash history completely?

I want to clear all previous commands from the history of my server. I used history -c and it seems all things are cleared but when I ssh to the server, all the commands are still there. How can I clear them permanently?
hd.
  • 4,087
1
2 3
99 100