The shell is unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …).

learn more… | top users | synonyms (1)

439
votes
3answers
83k views

What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?

I think these terms almost refer to the same thing, when used loosely: terminal shell tty console What exactly does each of these terms refer to?
162
votes
5answers
9k views

What's the difference between $(stuff) and `stuff`?

There are two syntaxes for command substitution: with dollar-parentheses and with backticks. Running top -p $(pidof init) and top -p `pidof init` gives the same output. Are these two ways of doing the ...
145
votes
2answers
27k views

Difference between nohup, disown and &

What is the difference between $ nohup foo and $ foo & and $ foo & $ disown
115
votes
10answers
39k views

Turn off buffering in pipe

I have a script which calls two commands: long_running_command | print_progress The long_running_command prints a progress but I'm unhappy with it. I'm using print_progress to make it more nice ...
112
votes
15answers
54k views

How to do integer & float calculations, in bash or other languages/frameworks?

Using echo "20+5" literally produces the text "20+5" What command can I use to get the numeric sum, e.g. 25 in this case. Also, what's the easiest way to do it just using bash for floating point, ...
90
votes
6answers
7k views

How to run a command without hitting Enter Key?

Accidently I mapped Enter via xkbset to Pointer_button2. Now every time I hit Enter some gibberish text appears. I thought of a workaround involving remapping it back, but that means I will have to ...
85
votes
3answers
8k views

Why is “echo” so much faster than “touch”?

I'm trying to update the timestamp to the current time on all of the xml files in my directory (recursively). I'm using Mac OSX 10.8.5. On about 300,000 files, the following echo command takes 10 ...
82
votes
3answers
5k views

Why not use “which”? What to use then?

When looking for the path to an executable or checking what would happen would you enter a command name in a Unix shell, there's a plethora of different utilities (which, type, command, whence, where, ...
79
votes
6answers
7k views

Why is cd not a program?

I've always wondered why cd isn't a program, but never managed to find the answer. Anyone know why this is the case?
77
votes
8answers
10k views

Why *not* parse `ls`?

I consistently see answers quoting this link stating definitively "Don't parse ls!" This bothers me for a couple of reasons: It seems the information in that link has been accepted wholesale with ...
76
votes
8answers
88k views

Colorizing your terminal and shell environment?

I spend most of my time working in Unix environments and using terminal emulators. I try to use color on the command-lin, because color makes the output more useful and intuitive. What are some good ...
74
votes
2answers
19k views

What does “--” (double-dash) mean? (also known as “bare double dash”)

I have seen -- used in the compgen command. For example: compgen -W "foo bar baz" -- b What is the meaning of the --.
73
votes
4answers
1k views

Can I somehow add a “&& prog2” to an already running prog1?

Most shells provide functions like && and ; to chain the execution of commands in certain ways. But what if a command is already running, can I still somehow add another command to be executed ...
73
votes
6answers
80k views

How to split the terminal into more than one “view”?

From vi, if you issue the command :sp, the screen splits into two "views", allowing you to edit more than one file from the same terminal. Along those same lines, is there a way to have multiple ...
71
votes
6answers
3k views

Run a command that is shadowed by an alias

Let's say i have the following alias in bash - alias ls='ls --color=auto' - and i want to call ordinary ls without options. Is the only way to do that is to unalias, do the command and then alias ...
61
votes
25answers
10k views

Quick directory navigation in the terminal

I would like to frequently switch between directories that are in totally unrelated paths, for example /Project/Warnest/docs/ and ~/Dropbox/Projects/ds/test/. But I don't want to type cd ...
61
votes
5answers
36k views

Allow setuid on shell scripts

The setuid permission bit tells Linux to run a program with the effective user id of the owner instead of the executor: > cat setuid-test.c #include <stdio.h> #include <unistd.h> int ...
57
votes
12answers
21k views

Get exit status of process that's piped to another

I have two processes foo and bar, connected with a pipe: $ foo | bar bar always exits 0; I'm interested in the exit code of foo. Is there any way to get at it?
53
votes
13answers
48k views

How can I get distribution name and version number in a simple shell script?

I'm working on a simple bash script that should be able to run on Ubuntu and CentOS distributions (support for Debian and Fedora/RHEL would be a plus) and I need to know the name and version of the ...
49
votes
4answers
6k views

What is the difference between “&&” and “;” when chaining commands

What is the difference between the following methods of chaining commands? cmd1; cmd2 cmd1 && cmd2
49
votes
6answers
1k views

Resources for portable shell programming

What resources exist for portable shell programming? The ultimate answer is to test on all targeted platforms, but that's rarely practical. The POSIX / Single UNIX specification is a start, but it ...
48
votes
8answers
11k views

How do I delete a file whose name begins with “-” (hyphen a.k.a. dash or minus)?

How do you remove a file whose filename begins with a dash (hyphen or minus) -? I'm ssh'd into a remote OSX server and I have this file in my directory: tohru:~ $ ls -l total 8 -rw-r--r-- 1 me ...
48
votes
7answers
51k views

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file. So I was wondering: How to add a newline at the end of a file to get rid off those messages?
48
votes
2answers
3k views

Shebang starting with `//`?

I'm confused about following script (hello.go). //usr/bin/env go run $0 $@ ; exit package main import "fmt" func main() { fmt.Printf("hello, world\n") } It can execute. (on MacOS X 10.9.5) $ ...
46
votes
2answers
2k views

Is >&- more efficient than >/dev/null?

Yesterday I read this SO comment which says that in the shell (at least bash) >&- "has the same result as" >/dev/null. That comment actually refers to the ABS guide as the source of its ...
46
votes
4answers
18k views

Difference between Login Shell and Non-Login Shell?

I understand the basic difference between an interactive shell and a non-interactive shell. But what exactly differentiates a login shell from a non-login shell? Can you give examples for uses of a ...
44
votes
5answers
2k views

cd by just typing the directory's name?

I either had this somewhere 20 years ago or I dreamed about it. Basically: If if type blobblob I get blobblob: command not found Fair enough. I would like it so that when my shell gets those ...
43
votes
5answers
4k views

Redirecting stdout to a file you don't have write permission on

When you attempt to modify a file without having write permissions on it, you get an error: > touch /tmp/foo && sudo chown root /tmp/foo > echo test > /tmp/foo zsh: permission ...
42
votes
4answers
4k views

Why does no one use the true Bourne shell as /bin/sh?

I've noticed that basically no system I've ever worked with has /bin/sh as a real executable. It's always a symlink to dash, bash in POSIX mode, or something similar. Why? What are the disadvantages ...
41
votes
6answers
38k views

How do you move all files (including hidden) in a directory to another?

How do I move all files in a directory (including the hidden ones) to another directory? For example, if I have a folder "Foo" with the files ".hidden" and "notHidden" inside, how do I move both ...
41
votes
4answers
87k views

How do I grep for multiple patterns?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and ...
40
votes
7answers
32k views

What's the difference between /sbin/nologin and /bin/false

Technically, unless pam is set up to check your shell with pam_shells neither of these can actually prevent your login, if you're not on the shell. On my system they are even different sizes, so I ...
39
votes
12answers
8k views

best way to search my shell's history

Is there a better way to search my history file for a command than grep? I do have some idea what the command starts as, but I don't know how far back in the history it is. update: was formerly zsh ...
38
votes
2answers
4k views

$VAR vs ${VAR} and to quote or not to quote

I can write VAR=$VAR1 VAR=${VAR1} VAR="$VAR1" VAR="${VAR1}" the end result to me all seems about the same. Why should I write one or the other? are any of these not portable/POSIX?
37
votes
4answers
4k views

How to use `which` on an aliased command?

Like most users, I have a bunch of aliases set up to give a default set of flags for frequently used programs. For instance, alias vim='vim -X' alias grep='grep -E' alias ls='ls -G' The problem is ...
36
votes
4answers
17k views

Why is `while IFS= read` used so often, instead of `IFS=; while read..`?

It seems that normal practice would put the setting of IFS outside the while loop in order to not repeat setting it for each iteration... Is this just a habitual "monkey see, monkey do" style, as it ...
35
votes
6answers
64k views

How to overwrite target files with mv?

I have a ton of files and dirs in a subdirectory I want to move to the parent directory. There are already some files and dirs in the target directory which need to be overwritten. Files that are only ...
35
votes
4answers
1k views

What exactly is an environment variable?

I know that VARIABLE=value creates an environment variable, and export VARIABLE=value makes it available to processes created by the current shell. env shows the current environment variables, but ...
34
votes
2answers
2k views

How do I open an incognito bash session?

Is it possible to open an incognito session in bash? For example, when we need to enter passwords in commands and don't want bash to add them to history.
34
votes
4answers
19k views

How can I send stdout to multiple commands?

There are some commands which filter or act on input, and then pass it along as output, I think usually to stdout - but some commands will just take the stdin and do whatever they do with it, and ...
34
votes
2answers
2k views

Why is '.' a hard link in Unix?

I've seen many explanations for why the link count for an empty directory in Unix based OSes is 2 instead of 1. They all say that it's because of the '.' directory, which every directory has pointing ...
33
votes
5answers
3k views

What are the fundamental differences between the mainstream *NIX shells?

What are the fundamental differences between the mainstream *NIX shells and what scenarios might prompt you to use one over the other? I understand that some of it probably comes down to user ...
33
votes
7answers
16k views

How to test what shell I am using in a terminal?

How to check what shell I am using in a terminal? What is the shell I am using in MacOS?
33
votes
5answers
13k views

How to check if a shell is login/interactive/batch

I think I understand the differences between an interactive, a login and a batch shell. See the following links for more help: What is the difference between a 'Login' and an 'Interactive' bash ...
33
votes
5answers
6k views

Timing out in a shell script

I have a shell script that's reading from standard input. In rare circumstances, there will be no one ready to provide input, and the script must time out. In case of timeout, the script must execute ...
32
votes
4answers
3k views

What is the difference between a builtin command and one that is not?

Is there any intrinsic difference between a builtin command and another command which can nominally do the same thing? eg. Do builtins get "special" treatement? ... is there less overhead running ...
32
votes
8answers
3k views

Output “Hollywood-hacker-scene” from a shell

This might seem like a strange question, but what would you use for creating the typical hollywood hacking scene? I got this request from a friend and my brain was blank. All I could think of was ...
32
votes
5answers
39k views

How to monitor CPU/memory usage of a single process?

I would like to monitor one process's memory / cpu usage in real time. Similar to top but targeted at only one process, preferably with a history graph of some sort.
30
votes
7answers
4k views

why does ls -d also list files, and where is it documented?

when specifying ls --directory a* it should list only directories starting with a* BUT it lists files AND directories starting with a Questions: where might I find some documentation on this, ...
30
votes
10answers
13k views

How do I reuse the last output from the command line?

This is a noob question, but I'd like to know how to reuse the last output from the console, ie: pv-3:method Xavier$ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" ...