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

learn more… | top users | synonyms (1)

463
votes
34answers
222k views

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 -...
433
votes
12answers
525k views

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 ...
194
votes
5answers
76k views

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 ...
142
votes
39answers
79k views

What useful things can one add to one's .bashrc? [closed]

Is there anything that you can't live without and will make my life SO much easier? Here are some that I use ('diskspace' & 'folders' are particularly handy). # some more ls aliases alias ll='ls -...
125
votes
9answers
72k views

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 ...
121
votes
27answers
46k views

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 ...
113
votes
10answers
162k views

How do I get current Unix time in milliseconds using bash?

How do I get the current Unix time in milliseconds (i.e number of milliseconds since Unix epoch January 1 1970)?
111
votes
85answers
16k views

Linux command line best practices and tips?

I would like to open a discussion that would accumulate your Linux command line (CLI) best practices and tips. I've searched for such a discussion to share the below comment but haven't found one, ...
79
votes
10answers
49k views

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?
72
votes
6answers
74k views

How to run a command multiple times?

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, please do not post scripts. Update:...
71
votes
6answers
56k views

How to test if my server is vulnerable to the ShellShock bug?

How can I ensure my Bash installation is not vulnerable to the ShellShock bug anymore after the updates?
65
votes
5answers
87k views

Clean way to write complex multi-line string to a variable

I need to write some complex xml to a variable inside a bash script. The xml needs to be readable inside the bash script as this is where the xml fragment will live, it's not being read from another ...
64
votes
13answers
93k views

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 <something is mounted at /mnt/foo> then <Do some ...
62
votes
2answers
19k views

Colors in bash after piping through less?

When I have dircolors defined life is full of... color. When I pipe ls through less to scroll around I lose the colors. Any suggestions?
60
votes
4answers
16k views

Is there a difference between how two ampersands and a semi-colon operate in bash?

If I wanted to run two separate commands on one line, I could do this: cd /home; ls -al or this: cd /home && ls -al And I get the same results. However, what is going on in the ...
60
votes
10answers
17k views

Unique Features of bash compared to zsh

I have been a zsh user for quite some time (before that tcsh and before that csh). I am quite happy with it, but was wondering if there are any compelling features of bash that do not exist in zsh. ...
59
votes
7answers
75k views

mysqldump to a tar.gz

Usually after dumping a MySQL database with mysqldump command I immediately tar/gzip the resultant file. I'm looking for a way to do this in one command: So from this: mysqldump dbname -u root -p &...
56
votes
12answers
32k views

How can I run arbitrarily complex command using sudo over ssh?

I have a system that I can only log in to under my username (myuser), but I need to run commands as other user (scriptuser). So far, I have come up with the following to run the commands I need: ssh -...
56
votes
8answers
19k views

bash: print stderr in red color

Is there a way to make bash display stderr messages in red color?
55
votes
3answers
20k views

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

What are the functional differences between the .profile, .bash_profile and .bashrc files?
54
votes
12answers
67k views

What are useful .screenrc settings?

Basically like some of my own that I've posted below. I'm looking for added functionality to the programme 'screen'. At the very least have a look at the last line for a fantastic 'menu bar' at the ...
52
votes
8answers
51k views

How to make bash scripts print out every command before it executes?

For example, I have a simple bash file #!/bin/bash cd ~/hello ls How can I make it display every command before executing it? Just the opposite effect of "@echo off" in windows batch scripting.
49
votes
6answers
114k views

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 ? ...
40
votes
1answer
11k views

What is the difference between a 'Login' and an 'Interactive' bash shell

What is the difference between a ‘Login’ and an ‘Interactive’ bash shell? I have quoted Wikipedia below but can anybody give a better answer? EDIT: This is a community wiki so maybe rather than ...
38
votes
6answers
43k views

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-...
38
votes
8answers
71k views

How to add a timestamp to bash script log?

I have a constantly running script that I output to a log file: script.sh >> /var/log/logfile I'd like to add a timestamp before each line that is appended to the log. Like: Sat Sep 10 21:...
38
votes
7answers
31k views

Log all commands run by admins on production servers

It is company policy for admins to login to the servers via a personal username, and then run sudo -i to become root. Upon running sudo -i, sudo will create an environmental variable called SUDO_USER,...
37
votes
6answers
11k views

Can you have more than one ~/.ssh/config file?

We have a bastion server that we use to connect to multiple hosts, and our .ssh/config has grown to over a thousand lines (we have hundreds of hosts that we connect to). This is beginning to get a ...
37
votes
4answers
30k views

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 ...
36
votes
3answers
6k views

Run command in bash without save in history

How to run command in bash without saving it in history?
34
votes
4answers
41k views

run script as user who has nologin shell

All I need to do is to run a specific script as a particual user who does have the nologin/false shell indicated in /etc/passwd. I would run the script as root and this should run as another user. ...
33
votes
7answers
101k views

How can I edit the welcome message when ssh start?

I have a vps for my website hosting. It is running a ubuntu server. Every time I logged in my server by ssh, it displays a lengthy welcome message in my terminal. Linux node61.buyvm.net 2.6.18-...
31
votes
6answers
291k views

How to pass password to scp command used in bash script? [duplicate]

Possible Duplicate: Connect through SSH and type in password automatically, without using a public key I have a bash script that makes dump of DB then copies file from one server to another ...
31
votes
6answers
53k views

How to get pid of just started process

I want to start process (eg. myCommand) and get its pid (to allow to kill it later). I tried ps and filter by name, but I can not distinguish process by names myCommand ps ux | awk '/<myCommand&...
31
votes
6answers
22k views

Merge 2 directory trees in Linux without copying?

I have two directory trees with similar layouts, i.e. . |-- dir1 | |-- a | | |-- file1.txt | | `-- file2.txt | |-- b | | `-- file3.txt | `-- c | `-- file4.txt `-- ...
31
votes
3answers
6k views

In Bash, are wildcard expansions guaranteed to be in order?

Is the expansion of a wildcard in Bash guaranteed to be in alphabetical order? I am forced to split a large file into 10 Mb pieces so that they can be be accepted by my Mercurial repository. So I was ...
30
votes
7answers
44k views

How do I run a local bash script on remote machines via ssh?

I am looking for a way to push configuration from one central machine to several remote machines without the need to install anything on the remote machines. The aim is to do something like you ...
29
votes
5answers
2k views

Avoid to keep command in history

I use bash and I would like to avoid some commands being kept in the history. Is it possible to do that for the next command only? Is it possible to do that for the entire session?
28
votes
3answers
3k views

Did a cat /dev/urandom - Broken terminal

Out of curiosity, when your shell character set breaks from doing something like cat /dev/urandom is there a way to fix that in place?
28
votes
8answers
14k views

bash/sed/awk/etc remove every other newline

a bash commands outputs this: Runtime Name: vmhba2:C0:T3:L14 Group State: active Runtime Name: vmhba3:C0:T0:L14 Group State: active unoptimized Runtime Name: vmhba2:C0:T1:L14 Group State: active ...
28
votes
2answers
22k views

How to apply a filter to real time output of `tail -f `?

tail -f path The above will output modifications to the file instantly, but I want to apply a filter to the output, only show when there is a keyword xxx in it. How to approach this?
28
votes
4answers
12k views

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;...
27
votes
3answers
58k views

How to accurately check if package is installed in yum?

I keep getting answers like: yum list installed | grep bind or rpm -qa | grep bind But that is not accurate as I'm getting a list of few other bind packages like these: bind-utils-9.8.2-0.17....
27
votes
7answers
21k views

Using ctrl-arrow keys with PuTTY and screen

I searched and couldn't find a solution for this anywhere. I'm using PuTTY from Windows to connect to various servers where I run bash and screen. It seems bash works fine with ctrl-arrow keys to ...
26
votes
5answers
16k views

Piping wget output to /dev/null in cron

I am running the following command every 5 minutes in my crontab to keep Phusion Passenger alive. */5 * * * * wget mysite.com > /dev/null 2>&1 When I run this it performs a wget on the ...
25
votes
12answers
12k views

Linux: How to use a file as input and output at the same time?

I've just run the following in bash: uniq .bash_history > .bash_history and my history file ended up completely empty. I guess I need a way to read the whole file before writing to it. How is ...
25
votes
6answers
12k views

Bash Loop - How to stop the loop when I press Control-C inside a command?

I am rsyncing a few directories. I have a bash terminal open and am executing something like this: for DIR in * ; do rsync -a $DIR example.com:somewhere/ ; done However if I want to stop the whole ...
25
votes
7answers
14k views

Can't log in via SSH to any accounts using /bin/bash shell on Synology NAS

I'm trying to install bash as the default shell on a ARM Linux running on an embedded device (Synology DS212+ NAS). But there's something really wrong, and I can't figure out what it is. Symptoms: 1)...
24
votes
6answers
20k views

How to read in N random characters from /dev/urandom?

read /dev/urandom 3 The above is not working..How can I read random bytes from /dev/urandom in bash?
24
votes
9answers
64k views

How do I find out whether a port is available on Ubuntu 8.04?

Is there any command I can run from bash that will tell me whether a port is already open?