You seem to want to remove everything after the first dot in the names available in the current directory.
To loop over all the names in the current directory, use
for name in *; do ...; done
Using the output of ls
for anything other than for visual inspection is error prone and should be avoided. See Why *not* parse `ls` (and what do to instead)?
To remove the bit after the first dot in $name
(including the dot itself), use ${name%%.*}
. This is a standard parameter expansion that removes the longest suffix matching the globbing pattern .*
from the value of the variable name
.
To output the result of the this expansion, you may use
printf '%s\n' "${name%%.*}"
See both When is double-quoting necessary? and Why is printf better than echo?.
Your full script could be written
#!/bin/sh
for name in *; do
printf '%s\n' "${name%%.*}"
done
Your second question, about what ${i}
is called: It's a variable expansion (sometimes the terms "parameter expansion" or "variable/parameter substitution" are used). The ${i}
will be expanded to/substituted by the value of the variable i
. This could also be written $i
as the curly braces do not do anything special here. The only place where you may want to use ${i}
instead of $i
is where the expansion is part of a string and the very next character is valid in a variable's name, as in "${i}_stuff"
($i_stuff
would try to expand the variable i_stuff
).
for i in
ls;do echo ${i} | cut -d "." -f1;done
it will work – shas Jul 25 '19 at 11:57