I have a bash code which assigns all files into a variable, splits the string by a space and loops over them like this:
$ ls
README db_password.example db_username.example
files_str=$(ls)
files=(${files_str// /})
for file in "${files[@]}"; do
if [ "$file" != "README" ]; then
echo "$file is not README";
fi;
done;
output:
I think now that it is something with data types. I thought bash only has strings? I guess I was mislead, maybe by similar sources like this: https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-5.html#:~:text=There%20are%20no%20data%20types,its%20reference%20will%20create%20it.
But when I do
file=README
if [ "$file" != "README" ]; then
echo "$file is not README";
fi;`
I get no output which is correct. So how do I make it work with ls
?
https://shellcheck.net
, a syntax checker, or installshellcheck
locally. Make usingshellcheck
part of your development process.