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

learn more… | top users | synonyms (1)

257
votes
33answers
93k 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 ...
214
votes
11answers
250k views

How to determine if a bash variable is empty?

What is the best way to determine if a variable in bash contains ""? I have heard it recommended that I do if [ "x$variable" = "x" ] Is that the correct way? (there must be something more ...
116
votes
3answers
40k 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 ...
91
votes
24answers
28k 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 ...
90
votes
9answers
29k 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 ...
69
votes
9answers
93k 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)?
68
votes
6answers
45k 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?
54
votes
10answers
16k 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. ...
51
votes
5answers
53k 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 ...
40
votes
7answers
46k 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 ...
38
votes
12answers
51k 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 ...
38
votes
2answers
10k 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? Thanks, -Mathew
37
votes
4answers
35k 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. ...
37
votes
3answers
12k views

.profile vs. .bash_profile vs. .bashrc

what are the functional differences between these guys?
36
votes
8answers
11k views

bash: print stderr in red color

Is there a way to make bash display stderr messages in red color?
34
votes
1answer
7k 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 ...
33
votes
8answers
5k 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 ...
30
votes
7answers
20k views

How to create a UUID in bash?

In Java i can create a new random UUID with UUID uuid = UUID.randomUUID(); I now need to do the same within a bash script. Is there a command or library I could use?
29
votes
3answers
7k 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 ...
29
votes
8answers
25k 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.
29
votes
6answers
19k 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 `-- ...
28
votes
3answers
4k views

Run command in bash without save in history

How to run command in bash without saving it in history?
25
votes
3answers
2k 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?
23
votes
9answers
52k 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?
22
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?
21
votes
7answers
6k 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 ...
21
votes
1answer
349 views

Is there a device file that just blocks?

... something similar to /dev/null, but that just blocks all bytes that are written to it? What I need is a trick to hold an arbitrary executable in memory by blocking its output. Pipes don't seem ...
20
votes
5answers
923 views

What could make `>` silently fail in Linux?

I ran this command: python ./manage.py dumpdata partyapp.InvitationTemplate > partyapp_dump.json To dump data into the partyapp_dump.json file. But all the data just gets printed on the screen ...
20
votes
6answers
163k 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 ...
20
votes
7answers
13k 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 ...
19
votes
4answers
3k views

Ordinary users are able to read /etc/passwd, is this a security hole?

ls -l /etc/passwd gives $ ls -l /etc/passwd -rw-r--r-- 1 root root 1862 2011-06-15 21:59 /etc/passwd So an ordinary user can read the file. Is this a security hole?
19
votes
6answers
18k views

Adding 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:33:06 ...
19
votes
2answers
5k views

Why does redirecting 'script' to /dev/null/ allow 'screen' to work while su'ed as another user?

I was su'ed into a user to run a particular long running script. I wanted to use screen but I got the error message "Cannot open your terminal '/dev/pts/4' - please check." So I Googled around and ...
19
votes
3answers
8k views

How can I check in bash if a shell is running in interactive mode?

How can I tell (in ~/.bashrc) if I'm running in interactive mode, or, say, executing a command over ssh. I want to avoid printing of ANSI escape sequences in .bashrc if it's the latter.
18
votes
5answers
16k 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 ...
18
votes
8answers
6k views

Is there any reason to use bash over zsh?

I'm curious about why one would want to run bash instead of zsh. I mean zsh is fully backwards-compatible with bash. Don't get me wrong: I don't dislike bash or anything. I just genuinely want to ...
18
votes
1answer
1k views

Shell command slow when using pipe, fast with intermediate file

Does anyone understand this huge difference in processing time, when using an intermediate file, or when using a pipe? I'm converting tiff to pdf using standard tools on a fresh debian squeeze server. ...
17
votes
14answers
2k views

Reasons for missing IP info in `last` output on pts logins?

I have five CentOS 6 linux systems at work, and encountered a rather strange issue that only seems to happen with my userid across all the linux systems I have... This is an example of the problem ...
17
votes
7answers
16k 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 ...
17
votes
6answers
8k 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 ...
17
votes
1answer
1k views

Ctrl-C in bash scripts

How do I implement Ctrl-C handling in bash scripts so that the script interrupts as well as the currently running command, launched by it? Imagine there's a script that executes some long-running ...
17
votes
6answers
15k views

If I launch a background process and then log out, will it continue to run?

Asking this after a prolonged discussion with a coworker, I'd really like a clarification here. I launch a background process, either by appending "&" to the command line or by stopping it with ...
17
votes
6answers
7k views

Can't log in via SSH to any accounts set to use /bin/bash as a default shell

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: ...
16
votes
7answers
2k views

Can awk be used instead?

I would like to get the number from rating as output from this # nc localhost 9571 language: language:en_ZA.UTF-8 language:en_ZW.UTF-8 session-with-name:Ubuntu Classic (No effects):gnome-session ...
16
votes
4answers
21k views

How do I patch RHEL 4 for the bash vulnerabilities in CVE-2014-6271 and CVE-2014-7169?

A mechanism for remote code execution through Bash has been widely reported yesterday and today (September 24, 2014.) http://seclists.org/oss-sec/2014/q3/650 Reported as CVE-2014-7169 or CVE-2014-6271 ...
16
votes
6answers
6k 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 ...
16
votes
2answers
1k views

Is there a bash builtin for 'which'?

I've been testing a minimal Fedora install. To check the path for interpreters like python or node, I normally use which. I notice which isn't installed by default. I could add the package, but I ...
16
votes
4answers
4k 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 ...
15
votes
5answers
16k views

How do I pipe a downloaded file to standard output in bash?

How is it possible to pipe out wget's downloaded file? If not what alternatives should I use? UPD: Thanks all for your answers, I managed to tune my downloader (curl) to send files to stout. However ...
15
votes
5answers
2k 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 ...