Linked Questions

3
votes
2answers
1k views

Why I should avoid loops in shells? [duplicate]

The following link recommends against using loops in shells. bash variables in for loop range Why is this? Here is an example loop I just happened to be looking at when I came across that answer: ...
0
votes
3answers
485 views

Can’t echo variable value=* in script [duplicate]

Content of myfile: 123 **1 ** Script that attempts to display each word: for i in $(cat $myfile) do echo "$i" done the result is when echo *, it lists the files in my current ...
134
votes
9answers
281k views

How do I trim leading and trailing whitespace from each line of some output?

I would like to remove all leading and trailing spaces and tabs from each line in an output. Is there a simple tool like trim I could pipe my output into? Example file: test space at back test ...
84
votes
18answers
211k views

How to count the number of a specific character in each line?

I was wondering how to count the number of a specific character in each line by some text processing utilities? For example, to count " in each line of the following text "hello!" Thank you! The ...
123
votes
9answers
171k views

How to remove duplicate lines inside a text file?

A huge (up to 2 GiB) text file of mine contains about 100 exact duplicates of every line in it (useless in my case, as the file is a CSV-like data table). What I need is to remove all the repetitions ...
166
votes
6answers
18k views

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 ...
203
votes
3answers
17k 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 ...
51
votes
10answers
18k views

What's the fastest way to generate a 1 GB text file containing random digits?

I tried a bash script, but it took too long to create a simple 1 MB file. I think the answer lies in using /dev/random or /dev/urandom, but other posts here only show how to add all kinds of data to a ...
53
votes
6answers
5k views

Is there any reason to have a shebang pointing at /bin/sh rather than /bin/bash?

In most shell scripts I've seen (besides ones I haven't written myself), I noticed that the shebang is set to #!/bin/sh. This doesn't really surprise me on older scripts, but it's there on fairly new ...
38
votes
15answers
21k views

Object-oriented shell for *nix

Preface: I love bash and have no intention of starting any sort of argument or holy-war, and hopefully this is not an extremely naive question. This question is somewhat related to this post on ...
22
votes
17answers
4k views

How to find line with least characters

I am writing a shell script, using any general UNIX commands. I have to retrieve the line that has the least characters (whitespace included). There can be up to around 20 lines. I know I can use ...
26
votes
14answers
77k views

Bash script to convert all *flac to *.mp3 with FFmpeg?

I want to convert all *.flac to *.mp3 in the specific folder. This is what I've tried, but not works: # change to the home directory cd ~/music # convert all *.flac files ffmpeg -i *.flac -acodec ...
44
votes
8answers
4k views

Why are POSIX mandatory utilities not built into the shell?

The purpose of this question is to answer a curiosity, not to solve a particular computing problem. The question is: Why are POSIX mandatory utilities not commonly built into shell implementations? ...
29
votes
8answers
6k views

Is there something wrong with my script or is Bash much slower than Python?

I was testing the speed of Bash and Python by running a loop 1 billion times. $ cat python.py #!/bin/python # python v3.5 i=0; while i<=1000000000: i=i+1; Bash code: $ cat bash2.sh #!/bin/...
55
votes
3answers
59k views

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 ...

15 30 50 per page