Linked Questions

-2
votes
2answers
7k views

Grep a variable in a file [duplicate]

A file is modified by a script using an input file- 141,141_1,BAR,HONDA,ps2_0,unassigned,ps3_0,Unassigned,ps4_0,Unassigned,ps5_0,Unassigned,ps6_0,Unassigned,ps7_3,TILL WILL,..... Input file- ...
1
vote
1answer
1k views

Passing a variable with spaces from bash to awk [duplicate]

I am trying to pass a variable that contains spaces from bash to awk. I have simple a simple script for doing this with single string variables (in which I am searching the first column of the FILE ...
0
votes
1answer
210 views

Bash script to tar – Quoting issue [duplicate]

I have a script that takes filenames as positional parameters. I perform a few operations on these and then tar them. Currently my script is not working. The echo line is there for debugging ...
263
votes
4answers
204k 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 ...
186
votes
4answers
35k 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, ...
44
votes
11answers
38k views

How do I reuse the last output from the command line?

I'd like to know how to reuse the last output from the console, ie: pv-3:method Xavier$ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()" /Library/Python/2.6/site-...
111
votes
1answer
39k 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 $...
46
votes
5answers
77k views

How do I recursively delete directories with wildcard?

I am working through SSH on a WD My Book World Edition. Basically I would like to start at a particular directory level, and recursively remove all sub-directories matching .Apple*. How would I go ...
33
votes
7answers
106k views

Can sed replace new line characters?

Is there an issue with sed and new line character? I have a file test.txt with the following contents aaaaa bbbbb ccccc ddddd The following does not work: sed -r -i 's/\n/,/g' test.txt ...
23
votes
8answers
7k views

How to pipe output from one process to another but only execute if the first has output?

How can I rewrite this command to only email if there is output from the mailq | grep? mailq | egrep 'rejected|refused' -A 5 -B 5 | mail -s 'dd' email@email Is this even possible on one line? See ...
41
votes
3answers
32k views

What does ${1+“$@”} mean in a shell script, and how does it differ from “$@”?

In the Perl documentation, perlrun(1) suggests launching Perl scripts using a bilingual shell/Perl header: #!/bin/sh #! -*-perl-*- eval 'exec perl -x -wS $0 ${1+"$@"}' if 0; What does ${1+"$@"} ...
23
votes
5answers
2k views

Why use double quotes in a [[ ]] test?

Let's say we have 2 integers in a bash script: value1=5 value2=3 Then why do we need to use double quotes in case of a test ? For example: if [[ "$value1" -eq "$value2" ]] Why not just use the ...
17
votes
3answers
2k views

Why do options in a quoted variable fail, but work when unquoted?

I read about that I should quote variables in bash, e.g. "$foo" instead of $foo. However, while writing a script, I found an a case where it works without quotes but not with them: wget_options='--...
28
votes
2answers
2k views

Why is `sort < “$f1” ` preferred over `sort — “$f1”`, and why is this preferred over `sort “$f1”`?

From https://unix.stackexchange.com/a/458074/674 Remember to use -- when passing arbitrary arguments to commands (or use redirections where possible). So sort -- "$f1" or better sort < "$f1" ...
24
votes
5answers
6k views

Glob with Numerical Order

I have this list of pdf files in a directory: c0.pdf c12.pdf c15.pdf c18.pdf c20.pdf c4.pdf c7.pdf c10.pdf c13.pdf c16.pdf c19.pdf c2.pdf c5.pdf c8.pdf c11.pdf c14.pdf c17.pdf c1.pdf ...

15 30 50 per page