All Questions

Filter by
Sorted by
Tagged with
375 votes
6 answers

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 ...
Gilles 'SO- stop being evil''s user avatar
278 votes
5 answers

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, ...
cuonglm's user avatar
  • 154k
267 votes
10 answers

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 ...
mikeserv's user avatar
  • 58.4k
710 votes
4 answers

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 ...
amphibient's user avatar
  • 12.5k
179 votes
1 answer

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 $...
kjo's user avatar
  • 15.4k
275 votes
4 answers

Security implications of forgetting to quote a variable in bash/POSIX shells

If you've been following 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
430 votes
3 answers

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 ...
terdon's user avatar
  • 242k
218 votes
8 answers

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 ...
don_crissti's user avatar
  • 82.9k
189 votes
6 answers

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 ...
Tim's user avatar
  • 102k
112 votes
1 answer

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 ...
Gilles 'SO- stop being evil''s user avatar
488 votes
5 answers

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, ...
Stéphane Chazelas's user avatar
151 votes
1 answer

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 ...
Zsolt Szilagy's user avatar
1602 votes
10 answers

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?
Lazer's user avatar
  • 35.4k
138 votes
4 answers

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

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 ...
Nick.h's user avatar
  • 6,343
110 votes
4 answers

Understanding "IFS= read -r line"

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
  • 7,556
540 votes
15 answers

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 ...
Aaron Digulla's user avatar
418 votes
17 answers

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 ...
Michael Durrant's user avatar
244 votes
15 answers

How to run a specific program as root without a password prompt?

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/...
LanceBaynes's user avatar
  • 40.2k
142 votes
4 answers

Is there a ".bashrc" equivalent file read by all shells?

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
  • 25.3k
561 votes
8 answers

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 ...
Igorio's user avatar
  • 7,539
20 votes
2 answers

Brackets in if condition: why am I getting syntax errors without whitespace?

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
139 votes
7 answers

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:...
Michael Mrozek's user avatar
127 votes
3 answers

How can I assign the output of a command to a shell variable?

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,418
94 votes
3 answers

Open a window on a remote X display (why "Cannot open display")?

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 ...
justin cress's user avatar
  • 1,341
216 votes
3 answers

$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?
xenoterracide's user avatar
265 votes
11 answers

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 ...
Michael Mrozek's user avatar
34 votes
3 answers

Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

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
  • 32.9k
431 votes
16 answers

In Bash, when to alias, when to script and when to write a function?

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.2k
193 votes
9 answers

How can I disown a running process and associate it to a new screen shell?

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,815
31 votes
1 answer

How to ensure that string interpolated into `sed` substitution escapes all metachars

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,077
814 votes
5 answers

What does "--" (double-dash) mean?

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
  • 29.7k
51 votes
1 answer

Is it possible to use `find -exec sh -c` safely?

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
96 votes
6 answers

Understanding UNIX permissions and file types

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,031
117 votes
2 answers

How do keyboard input and text output work?

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
67 votes
9 answers

How can I use variables in the LHS and RHS of a sed substitution?

I want to do: cat | sed 's/old_name/new_name/' > 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
494 votes
10 answers

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 ...
Astra's user avatar
  • 5,043
213 votes
6 answers

How can I execute `date` inside of a crontab job?

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
  • 45.4k
1369 votes
12 answers

How to correctly add a path to PATH?

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.4k
152 votes
3 answers

Have backticks (i.e. `cmd`) in *sh shells been deprecated?

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
  • 370k
72 votes
1 answer

What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?

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
80 votes
4 answers

How do I run 32-bit programs on a 64-bit Debian/Ubuntu?

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
412 votes
4 answers

What characters do I need to escape when using sed in a sh script?

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 ...
detly's user avatar
  • 5,170
77 votes
2 answers

Getting "Not found" message when running a 32-bit binary on a 64-bit system

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
  • 949
961 votes
10 answers

How can I replace a string in a file(s)?

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
  • 242k
5 votes
1 answer

Wi-Fi problems using ASUS USB-N13 adapter (realtek)

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
  • 1,968
128 votes
4 answers

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?

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,098
85 votes
2 answers

When is dd suitable for copying data? (or, when are read() and write() partial)

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
749 votes
9 answers

What do the numbers in a man page mean?

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,617
111 votes
4 answers

What is the difference between sourcing ('.' or 'source') and executing a file in bash?

What's the difference between executing a script like this: ./ and executing a script like this: . I tried a simple, two-line script to see if I could find if there was a ...
Nathan's user avatar
  • 1,243

15 30 50 per page
2 3 4 5