Questions tagged [shell]
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, …). For shell scripts with errors, please check them in http://shellcheck.net before posting here.
2,263 questions
378
votes
6
answers
422k
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
80k
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, ...
285
votes
10
answers
99k
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 ...
183
votes
1
answer
82k
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 $...
283
votes
3
answers
58k
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 ...
445
votes
3
answers
232k
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
...
213
votes
6
answers
345k
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 ...
512
votes
6
answers
101k
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, ...
201
votes
2
answers
222k
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 ...
1636
votes
10
answers
548k
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?
559
votes
15
answers
344k
views
How to turn off stdout buffering in a pipe?
I have a script which calls two commands:
long_running_command | print_progress
The long_running_command prints progress but I'm unhappy with it. I'm using print_progress to make it nicer (namely, I ...
433
votes
17
answers
671k
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, 25 in this case?
Also, what's the easiest way to do it just using bash for floating
point? For ...
592
votes
8
answers
358k
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 ...
223
votes
3
answers
111k
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?
139
votes
7
answers
28k
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 denied:...