Most Popular

1500 questions
268
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, legibility, performance and security. This answer…
cuonglm
  • 150,973
  • 38
  • 327
  • 406
268
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 little question, though I can pick out at least a…
mikeserv
  • 57,448
  • 9
  • 113
  • 229
268
votes
6 answers

redirecting to /dev/null

I'm reading an example bash shell script: #!/bin/bash # This script makes a backup of my home directory. cd /home # This creates the archive tar cf /var/tmp/home_franky.tar franky > /dev/null 2>&1 # First remove the old bzip2 file. Redirect…
JohnMerlino
  • 5,941
  • 11
  • 34
  • 38
267
votes
3 answers

Find command: how to ignore case?

I am looking for file "WSFY321.c" in a huge directory hierarchy. Usually I would use GNU find: find . -name "WSFY321.c" But I do not know the case, it could be uppercase, lowercase, or a mix of both. What is the easiest way to find this file? Is…
Nicolas Raoul
  • 7,945
  • 14
  • 43
  • 55
266
votes
18 answers

How can I grep in PDF files?

Is there a way to search PDF files using grep, without converting to text first in Ubuntu?
Dervin Thunk
  • 3,429
  • 4
  • 23
  • 21
266
votes
4 answers

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 exception) has a very special meaning…
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
266
votes
15 answers

Passing named arguments to shell scripts

Is there any easy way to pass (receive) named parameters to a shell script? For example, my_script -p_out '/some/path' -arg_1 '5' And inside my_script.sh receive them as: # I believe this notation does not work, but is there anything close to…
Amelio Vazquez-Reina
  • 40,169
  • 77
  • 197
  • 294
265
votes
12 answers

Is there a one-liner that allows me to create a directory and move into it at the same time?

I find myself repeating a lot of: mkdir longtitleproject cd longtitleproject Is there a way of doing it in one line without repeating the directory name? I'm on bash here.
methodofaction
  • 3,309
  • 4
  • 17
  • 10
265
votes
5 answers

How to suspend and bring a background process to foreground

I have a process originally running in the foreground. I suspended by Ctrl+Z, and then resume its running in the background by bg . I wonder how to suspend a process running in the background? How can I bring a background process to…
Tim
  • 98,580
  • 191
  • 570
  • 977
265
votes
13 answers

Delete the last character of a string using string manipulation in shell script

I would like to delete the last character of a string, I tried this little script : #! /bin/sh t="lkj" t=${t:-2} echo $t but it prints "lkj", what I am doing wrong?
user3581976
  • 3,095
  • 3
  • 18
  • 19
261
votes
4 answers

How to tell gzip to keep original file?

I would like to compress a text file using gzip command line tool while keeping the original file. By default running the following command gzip file.txt results in modifying this file and renaming it file.txt.gz. instead of this behavior I would…
Manuel Selva
  • 3,454
  • 7
  • 20
  • 17
261
votes
21 answers

How do I get the MD5 sum of a directory's contents as one sum?

The md5sum program does not provide checksums for directories. I want to get a single MD5 checksum for the entire contents of a directory, including files in sub-directories. That is, one combined checksum made out of all the files. Is there a way…
user17429
258
votes
6 answers

VISUAL vs. EDITOR – what’s the difference?

I generally set both VISUAL and EDITOR environment variables to the same thing, but what's the difference? Why would I set them differently? When developing apps, why should I choose to look at VISUAL before EDITOR or vice versa?
xenoterracide
  • 57,918
  • 74
  • 184
  • 250
258
votes
12 answers

The "proper" way to test if a service is running in a script

My problem: I'm writing a bash script and in it I'd like to check if a given service is running. I know how to do this manually, with $ service [service_name] status. But (especially since the move to systemd) that prints a whole bunch of text…
Nick S
  • 2,725
  • 2
  • 11
  • 9
258
votes
3 answers

Why does `htop` show more process than `ps`

In ps xf 26395 pts/78 Ss 0:00 \_ bash 27016 pts/78 Sl+ 0:04 | \_ unicorn_rails master -c config/unicorn.rb 27042 pts/78 Sl+ 0:00 | \_ unicorn_rails worker[0] -c config/unicorn.rb …
Cheng
  • 6,531
  • 11
  • 40
  • 42