All Questions
26,338
questions
375
votes
6
answers
409k
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 ...
281
votes
5
answers
74k
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, ...
279
votes
10
answers
93k
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 ...
716
votes
4
answers
316k
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 ...
181
votes
1
answer
78k
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 $...
275
votes
4
answers
53k
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 ...
434
votes
3
answers
220k
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
...
219
votes
8
answers
41k
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 ...
197
votes
6
answers
299k
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 ...
112
votes
1
answer
20k
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 ...
490
votes
6
answers
93k
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, ...
156
votes
1
answer
118k
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 ...
1609
votes
10
answers
527k
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?
139
votes
4
answers
16k
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 ...
528
votes
9
answers
394k
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 ...