Frequent Questions

378 votes
6 answers
421k views

… 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 ...
Gilles 'SO- stop being evil''s user avatar
288 votes
5 answers
79k views

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, ...
cuonglm's user avatar
  • 158k
282 votes
10 answers
98k views

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 ...
mikeserv's user avatar
  • 59.4k
732 votes
4 answers
327k views

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 ...
amphibient's user avatar
  • 12.8k
182 votes
1 answer
81k views

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 $...
kjo's user avatar
  • 16.4k
282 votes
3 answers
57k views

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 ...
Stéphane Chazelas's user avatar
442 votes
3 answers
230k views

I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 ...
terdon's user avatar
  • 253k
220 votes
8 answers
45k views

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 ...
don_crissti's user avatar
  • 85.7k
213 votes
6 answers
340k views

$ 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 ...
Tim's user avatar
  • 107k
118 votes
1 answer
22k views

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 ...
Gilles 'SO- stop being evil''s user avatar
509 votes
6 answers
100k views

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, ...
Stéphane Chazelas's user avatar
196 votes
2 answers
210k views

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 ...
Zsolt Szilagy's user avatar
1634 votes
10 answers
545k views

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?
Lazer's user avatar
  • 36.2k
140 votes
4 answers
17k views

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 ...
Gilles 'SO- stop being evil''s user avatar
535 votes
9 answers
402k views

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 ...
Nick.h's user avatar
  • 6,443
115 votes
4 answers
161k views

I obviously understand that one can add value to internal field separator variable. For example: $ IFS=blah $ echo "$IFS" blah $ I also understand that read -r line will save data from stdin to ...
Martin's user avatar
  • 8,058
557 votes
15 answers
342k views

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 ...
Aaron Digulla's user avatar
253 votes
15 answers
560k views

I need to run something as sudo without a password, so I used visudo and added this to my sudoers file: MYUSERNAME ALL = NOPASSWD: /path/to/my/program Then I tried it out: $ sudo /path/to/my/program ...
LanceBaynes's user avatar
  • 41.6k
430 votes
17 answers
668k views

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 ...
Michael Durrant's user avatar
148 votes
4 answers
86k views

Is ~/.bashrc the only place to specify user specific environment variables, aliases, modifications to PATH variable, etc? I ask because it seems that ~/.bashrc seems to be bash-only, but other ...
Stefan's user avatar
  • 26.1k
35 votes
3 answers
32k views

Why do I get different values for $x from the snippets below? #!/bin/bash x=1 echo fred > junk ; while read var ; do x=55 ; done < junk echo x=$x # x=55 .. I'd expect this result x=1 cat ...
Peter.O's user avatar
  • 33.8k
592 votes
8 answers
356k views

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 ...
Igorio's user avatar
  • 7,937
139 votes
7 answers
28k views

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:...
Michael Mrozek's user avatar
129 votes
3 answers
397k views

I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/...
Nathan G.'s user avatar
  • 1,438
223 votes
3 answers
111k views

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?
xenoterracide's user avatar
20 votes
2 answers
12k views

I am using the below script to move two days back when script runs at starting two days of the year and also check first and second days of every month and move two days back. if [$month="01"...
Kumar1's user avatar
  • 341
94 votes
3 answers
183k views

Once upon a time, DISPLAY=:0.0 totem /path/to/movie.avi after ssh 'ing into my desktop from my laptop would cause totem to play movie.avi on my desktop. Now it gives the error: No protocol specified ...
justin cress's user avatar
  • 1,361
275 votes
11 answers
241k views

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 ...
Michael Mrozek's user avatar
441 votes
16 answers
90k views

Noone should need 10 years for asking this question, like I did. If I were just starting out with Linux, I'd want to know: When to alias, when to script and when to write a function? Where aliases are ...
ixtmixilix's user avatar
  • 13.6k
73 votes
1 answer
14k views

Sometimes I see shell scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used? Do ...
Michael Homer's user avatar
55 votes
1 answer
20k views

I'm trying to use find to echo 0 into some files, but apparently this only works with sh -c: find /proc/sys/net/ipv6 -name accept_ra -exec sh -c 'echo 0 > {}' \; But using sh -c with find -exec ...
Martin von Wittich's user avatar
33 votes
1 answer
9k views

I have a script that reads a text stream and generates a file of sed commands that is later run with sed -f. The generated sed commands are like: s/cid:image002\.gif@01CC3D46\.926E77E0/https:\/\/...
dan's user avatar
  • 4,137
195 votes
9 answers
158k views

I have a running program on a SSH shell. I want to pause it and be able to unpause its execution when I come back. One way I thought of doing that was to transfer its ownership to a screen shell, ...
levesque's user avatar
  • 3,935
846 votes
5 answers
393k views

I have seen -- used in the compgen command. For example: compgen -W "foo bar baz" -- b What is the meaning of the -- in there?
dogbane's user avatar
  • 30.8k
68 votes
9 answers
264k views

I want to do: cat update_via_sed.sh | sed 's/old_name/new_name/' > new_update_via_sed.sh in my program. But I want to use variables, e.g. old_run='old_name_952' new_run='old_name_953' I have ...
Michael Durrant's user avatar
517 votes
10 answers
228k views

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 ...
Astra's user avatar
  • 5,273
120 votes
2 answers
36k views

Suppose I press the A key in a text editor and this inserts the character a in the document and displays it on the screen. I know the editor application isn't directly communicating with the hardware (...
Gilles 'SO- stop being evil''s user avatar
104 votes
6 answers
111k views

I've never really got how chmod worked up until today. I followed a tutorial that explained a big deal to me. For example, I've read that you've got three different permission groups: owner (u) ...
Peter's user avatar
  • 1,111
1412 votes
12 answers
3.7m views

I'm wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it's not clear how to do this. This way: export ...
Paolo's user avatar
  • 17.9k
154 votes
3 answers
37k views

I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh. Is this ...
slm's user avatar
  • 380k
79 votes
2 answers
69k views

I have currently a strange problem on debian (wheezy/amd64). I have created a chroot to install a server (i can't give any more detail about it, sorry). Let's call its path /chr_path/. To make things ...
Elenaher's user avatar
  • 969
221 votes
6 answers
206k views

I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use: 0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log ...
cwd's user avatar
  • 47.1k
81 votes
4 answers
140k views

I have a 64-bit (amd64 a.k.a. x86_64) Debian or Ubuntu installation. I need to run 32-bit (i386/i686) programs occasionally, or to compile programs for a 32-bit system. How can I do this with a ...
Gilles 'SO- stop being evil''s user avatar
431 votes
4 answers
822k views

Take the following script: #!/bin/sh sed 's/(127\.0\.1\.1)\s/\1/' [some file] If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't need ...
detly's user avatar
  • 5,410
115 votes
4 answers
30k views

What's the difference between executing a script like this: ./test.sh and executing a script like this: . test.sh? I tried a simple, two-line script to see if I could find if there was a ...
Nathan's user avatar
  • 1,293
5 votes
1 answer
19k views

I have recently bought an ASUS Wi-Fi adapter (USB-N13), and I seem to be having some trouble with it. Every so often, the Wi-Fi connection just stops working, but the indicator says that I'm still ...
SpecialBomb's user avatar
  • 2,058
981 votes
11 answers
1.8m views

Replacing strings in files based on certain search criteria is a very common task. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub ...
terdon's user avatar
  • 253k
136 votes
4 answers
55k views

I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a ...
Majid Azimi's user avatar
  • 3,198
771 votes
9 answers
232k views

So, for example, when I type man ls I see LS(1). But if I type man apachectl I see APACHECTL(8) and if I type man cd I end up with cd(n). I'm wondering what the significance of the numbers in the ...
Wilduck's user avatar
  • 7,837
89 votes
2 answers
24k views

Short version: In what circumstances is dd safe to use for copying data, safe meaning that there is no risk of corruption due to a partial read or write? Long version — preamble: dd is often used to ...
Gilles 'SO- stop being evil''s user avatar

15 30 50 per page
1
2 3 4 5
558