Questions tagged [bash]

Bash is the Bourne Again SHell, the successor to the classic Unix sh (shell).

bash is the Bourne Again SHell, the successor to the classic Unix Bourne shell (sh). It's the default shell on many Linux distributions, including RedHat, CentOS, Debian, and Ubuntu.

The premier online guide is the BashGuide.

3462 questions
1347
votes
39 answers

How can I sort du -h output by size

I need to get a list of human readable du output. However, du does not have a "sort by size" option, and piping to sort doesn't work with the human readable flag. For example, running: du | sort -n -r Outputs a sorted disk usage by size…
Tom Feiner
  • 16,758
  • 8
  • 29
  • 24
922
votes
15 answers

How to determine if a bash variable is empty?

What is the best way to determine if a variable in bash is empty ("")? I have heard that it is recommended that I do if [ "x$variable" = "x" ] Is that the correct way? (there must be something more straightforward)
Brent
  • 22,219
  • 19
  • 68
  • 102
535
votes
5 answers

What is the difference between double and single square brackets in bash?

I just wondered what exactly the difference between [[ $STRING != foo ]] and [ $STRING != foo ] is, apart from that the latter is POSIX-compliant, found in sh and the former is an extension found in bash.
0x89
  • 6,345
  • 3
  • 21
  • 14
337
votes
19 answers

How do I get the current Unix time in milliseconds in Bash?

How do I get the current Unix time in milliseconds (i.e number of milliseconds since Unix epoch January 1 1970)?
Richard
  • 3,652
  • 2
  • 17
  • 13
326
votes
4 answers

What are the functional differences between .profile .bash_profile and .bashrc

What are the functional differences between the .profile, .bash_profile and .bashrc files?
lollercoaster
  • 3,425
  • 5
  • 17
  • 10
227
votes
10 answers

How to run a command multiple times, using bash shell?

Is there a way to run a command (e.g. ps aux|grep someprocess) for n times? Something like: run -n 10 'ps aux|grep someprocess' I want to use it interactively. Update: The reason I am asking this is, that I do work on a lot of machines and I don't…
mahatmanich
  • 2,794
  • 3
  • 21
  • 23
185
votes
14 answers

How to create a UUID in bash?

In Java it is possible to create a random UUID: UUID uuid = UUID.randomUUID(); How to do this in Bash?
raoulsson
  • 4,633
  • 10
  • 32
  • 29
181
votes
9 answers

Check if array is empty in Bash

I have an array which gets filled with different error messages as my script runs. I need a way to check if it is empty of not at the end of the script and take a specific action if it is. I have already tried treating it like a normal VAR and using…
Marcos Sander
  • 1,821
  • 2
  • 12
  • 5
177
votes
31 answers

How do I prevent accidental rm -rf /*?

I just ran rm -rf /* accidentally, but I meant rm -rf ./* (notice the star after the slash). alias rm='rm -i' and --preserve-root by default didn't save me, so are there any automatic safeguards for this? I wasn't root and cancelled the command…
Valentin Nemcev
  • 1,965
  • 2
  • 13
  • 12
172
votes
8 answers

How do I sleep for a millisecond in bash or ksh

sleep is a very popular command and we can start sleep from 1 second: # wait one second please sleep 1 but what the alternative if I need to wait only 0.1 second or between 0.1 to 1 second ? remark: on linux or OS X sleep 0.XXX works fine , but…
yael
  • 2,363
  • 4
  • 28
  • 41
167
votes
10 answers

What is "-bash: !": event not found"

Try executing the following under a bash shell echo "Reboot your instance!" On my installation: root@domU-12-31-39-04-11-83:/usr/local/bin# bash --version GNU bash, version 4.1.5(1)-release (i686-pc-linux-gnu) Copyright (C) 2009 Free Software…
Maxim Veksler
  • 2,555
  • 10
  • 27
  • 32
160
votes
10 answers

Run an interactive bash subshell with initial commands without returning to the ("super") shell immediately

I want to run a bash subshell, (1) run a few commands, (2) and then remain in that subshell to do as I please. I can do each of these individually: Run command using -c flag: $> bash -c "ls; pwd; " however, it immediately…
SABBATINI Luca
  • 1,601
  • 2
  • 11
  • 3
153
votes
18 answers

What's the best way to check if a volume is mounted in a Bash script?

What's the best way to check if a volume is mounted in a Bash script? What I'd really like is a method that I can use like this: if then else fi
Mark Biek
  • 1,857
  • 2
  • 14
  • 12
152
votes
11 answers

bash: print stderr in red color

Is there a way to make bash display stderr messages in red color?
kolypto
  • 10,738
  • 12
  • 51
  • 66
147
votes
9 answers

Keeping a linux process running after I logout

I'm connecting to a Linux machine through SSH, and I'm trying to run a heavy bash script that makes filesystem operations. It's expected to keep running for hours, but I cannot leave the SSH session open because of internet connections issues I…
doc_id
  • 1,499
  • 2
  • 12
  • 17
1
2 3
99 100