Frequent Questions
27,889 questions
378 votes
6 answers
421k views
Why does my shell script choke on whitespace or other special characters?
… or an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
288 votes
5 answers
79k views
Why is using a shell loop to process text considered bad practice?
Is using a while loop to process text generally considered bad practice in POSIX shells? As Stéphane Chazelas pointed out, some of the reasons for not using shell loop are conceptual, reliability, ...
282 votes
10 answers
98k views
Why *not* parse `ls` (and what to do instead)?
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 ...
732 votes
4 answers
327k views
Why is printf better than echo?
I have heard that printf is better than echo. I can recall only one instance from my experience where I had to use printf because echo didn't work for feeding some text into some program on RHEL 5.8 ...
182 votes
1 answer
81k views
When is double-quoting necessary?
The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
282 votes
3 answers
57k views
Security implications of forgetting to quote a variable in bash/POSIX shells
If you've been following unix.stackexchange.com for a while, you should hopefully know by now that leaving a variable unquoted in list context (as in echo $var) in Bourne/POSIX shells (zsh being the ...
442 votes
3 answers
230k views
What are the shell's control and redirection operators?
I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 ...
220 votes
8 answers
45k views
Why is looping over find's output bad practice?
This question is inspired by Why is using a shell loop to process text considered bad practice ? I see these constructs for file in `find . -type f -name ...`; do smth with ${file}; done and for ...
213 votes
6 answers
340k views
How can we run a command stored in a variable?
$ ls -l /tmp/test/my\ dir/ total 0 I was wondering why the following ways to run the above command fail or succeed? $ abc='ls -l "/tmp/test/my dir"' $ $abc ls: cannot access '"/tmp/test/my': No such ...
118 votes
1 answer
22k views
Why does my regular expression work in X but not in Y?
I wrote a regular expression which works well in a certain program (grep, sed, awk, perl, python, ruby, ksh, bash, zsh, find, emacs, vi, vim, gedit, …). But when I use it in a different program (or on ...
509 votes
6 answers
100k views
Why not use "which"? What to use then?
When looking for the path to an executable or checking what would happen if you enter a command name in a Unix shell, there's a plethora of different utilities (which, type, command, whence, where, ...
196 votes
1 answer
209k views
Understanding the -exec option of `find`
I find myself constantly looking up the syntax of find . -name "FILENAME" -exec rm {} \; mainly because I don't see how exactly the -exec part works. What is the meaning of the braces, the backslash ...
1634 votes
10 answers
545k 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?
140 votes
4 answers
17k views
Why are there so many different ways to measure disk usage?
When I sum up the sizes of my files, I get one figure. If I run du, I get another figure. If I run du on all the files on my partition, it doesn't match what df claims is used. Why are there so many ...
535 votes
9 answers
402k views
Execute vs Read bit. How do directory permissions in Linux work?
In my CMS, I noticed that directories need the executable bit (+x) set for the user to open them. Why is the execute permission required to read a directory, and how do directory permissions in Linux ...