Linked Questions
13 questions linked to/from Is there something wrong with my script or is Bash much slower than Python?
288
votes
5
answers
79k
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, ...
23
votes
3
answers
8k
views
What is the fastest way to run a script?
I was wondering what is the fastest way to run a script , I've been reading that there is a difference in speed between showing the output of the script on the terminal, redirecting it to a file or ...
15
votes
2
answers
8k
views
How do I shift a bash array at some index in the middle?
1 #!/bin/bash
2 # query2.sh
3
4 numbers=(53 8 12 9 784 69 8 7 1)
5 i=4
6
7 echo ${numbers[@]} # <--- this echoes "53 8 12 9 784 69 8 7 1" to stdout.
8 echo ${numbers[i]} # <--- this echoes ...
3
votes
7
answers
2k
views
Break down string into array in shell script
I am trying to convert string for example string=11111001 to array which I will be able to access by calling respective array index like
arr[0]=1, arr[1]=0
I am new to shell scripting and from what I ...
4
votes
4
answers
6k
views
Can I use comparison operators in case?
I'm building a function that will calculate the gauge of wire required given amperage, distance(in feet), and allowable voltage drop.
I can calculate the "circular mils" given those values and with ...
12
votes
2
answers
4k
views
What is the overhead of using subshells?
Hopefully this question is not too generic. I am very new to shell scripting and I come from a computer architecture/non-scripting programming background. I have noticed on the scripts at my work that ...
6
votes
1
answer
21k
views
Bash script inheritance? Calling a function from another script?
I have this line:
trap 'jobs -p | xargs kill -9' SIGINT SIGTERM EXIT
it is repeated in many bash shell scripts I have.
What is the best way to share this code? Can I call a bash function perhaps?
...
4
votes
1
answer
2k
views
bash: preallocate memory for array
Can memory be preallocated in bash for arrays of a defined size? I am iteratively adding strings to an array (declared by declare -a arr) in a loop (arr+=("$str")), and am wondering whether ...
0
votes
3
answers
2k
views
Using AWK To Extract Numbers From .CSV File
I have a .CSV file that I need to extract numbers from. Example of file:
#File of numbers
1,2,3,4,5
6,7,8,9,10
I want to use awk (or another utility) to extract the first number and second number and ...
0
votes
2
answers
2k
views
Insert variable in variable line using sed or awk [closed]
i have two variables (txt and a line number) i want to insert my txt in the x line
card=$(shuf -n1 shuffle.txt)
i=$(shuf -i1-52 -n1)
'card' is my txt : a card randomly selected in a shuffle 'deck'
...
1
vote
1
answer
800
views
For with read value
In what way does the final value of number being assigned by read var number (and we enter 5) and number=5 differ? I was making this for loop:
read var number
#number=5
factorial=1
i=1
for i in `seq ...
0
votes
1
answer
371
views
Comparing files in awk/bash
I came across a problem and have no idea how to find the OPTIMAL solution.
I have a list of files that looks like this:
file1\0file2\0...fileX\0\0file(x+1)\0
Every name of file is separated by a \0 ...
0
votes
1
answer
241
views
How do I write a word, letter by letter into my program's memory
I'm pretty new to unix and I'm wondering how I would go about writing a word, letter by letter into my program's memory. So for example if my word was "cup", I'd want an array A to have A[0] = 'c', A[...