I'm working on a quick script in bash where at the beginning, I set
IFS=$'\n'
. Right after I have a for loop:
for i in *;
do
j=${i//:/}
done
No matter what I do, a colon is never removed from $i
. I tried every method I could think of to remove characters from a string including sed
, tr
and more. I even tried using a backslash before the colon, or resetting IFS within the for loop.
The strange thing is that I can replace other characters, it's just the colon that's not working for me.
What should I do?
(Here's essentially the whole thing)
#!/bin/bash
IFS=$'\n'
for i in *;
do
one=${i//:/} ###no colons
two=${one::-4} ### remove file format
three=${two,,} ###make lowercase
four=${three// /-} ### replace spaces with dashes
five=${four//\'} ### remove any apostropes
six=${five//.} ### remove any periods
mkdir /home/"$whoami"/project/"$six"
done
IFS=$' \t\n'
${string/substring/replacement}
is Bash string expnasion syntax, which in this case is correct.IFS='\n'
sets Internal Field Seperator (i.e., word boundary) to newline, so bash can read escape sequences (newline) within the string. Default is space,tab, and newline.You're trying to read all file names in the current directory, and remove colon if they have any. The above shell script works perfectly fine in my computer (Ubuntu 22.04, Bash version 5.1.16):
❯ ls
fi:le1.txt file:2.txt file:multiple:3.jpg remove_colon.sh
❯ cat remove_colon.sh
#!/bin/bash
IFS=$'\n'
for i in *;
do
j=${i//:/}
echo "$j"
done
❯ ./remove_colon.sh
file1.txt
file2.txt
filemultiple3.jpg
remove_colon.sh