Why this shell script doesn't work? [duplicate] - linux

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 9 years ago.
n=5
for i in {1..$[n]}
do
echo $i
done
it gives:
{1..5}
But I think it should output:
1
2
3
4
5
Why it gives such a strange output?

That is almost a riddle. The expansion of the braces is being done prior to the variable expansion. The bash beginners guide has some good detail on expansion
There are a brazillion ways to do this in bash.
You could start with:
n=5
for i in $(eval echo {1..$n})
do
echo $i
done

Related

How to write result in empty variable? [duplicate]

This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Closed 2 years ago.
How to write command result into empty variable?
#!/bin/bash
today=''
$today = $(date)
echo $today
There shouldn't be a space around the =
On variable assignment, no need for the $
#!/bin/bash
today=''
today="$(date)"
echo "${today}"

Output exactly x number of characters to variable using read in Bash [duplicate]

This question already has answers here:
Extract substring in Bash
(26 answers)
Closed 6 years ago.
How to retrieve the first 10 characters of a variable with Bash?
FOO="qwertzuiopasdfghjklyxcvbnm"
I need to get qwertzuiop.
If the variable is: FOO="qwertzuiopasdfghjklyxcvbnm"
then
echo ${FOO:0:10}
will give the first 10 characters.
Use the head command.
echo $FOO | head -c 10
=> qwertzuiop

bash removes consecutive spaces from string [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 2 years ago.
I ran into a strange problem that I do not understand. Why are multiple spaces not present in the output of the following command?
$ d='A B'
$ echo $d
A B
use in double quotes:
echo "$d"

what does "$# -ne 4" mean in bash? [duplicate]

This question already has answers here:
What does "-ne" mean in bash?
(3 answers)
Closed 3 years ago.
i am new to shell, I met a code "$# -ne 4". "$#" means the number of command-line arguments that were passed to the shell program, then what does "-ne" mean?
-ne in bash denotes 'not equal to'

Escaping brackets in filename. Shell script [duplicate]

This question already has answers here:
is it safe to use "ls" in for loop in bash
(3 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have many files with pattern name:
N.apple(d).log
e.g. 001.apple(d).log, 002.apple(d).log, etc.
The problem is when I am trying to do something with the files in a bash script, there is always an error 'No such file or directory' even with escaped brackets:
for i in $(ls my_folder); do file=$(echo $i | sed 's/(/\\(/g' | sed 's/)/\\)/g') ; head -1 my_folder/$file; done
Thanks for any tips
Use quotes. Also, don't use ls when the shell can expand the wildcards just fine already.
for i in my_folder/*; do head -1 "$i"; done

Resources