How a Missing $ Made My Live‑Stream Shell Script Delete All .sh Files
During a live‑stream the author wrote a Bash script to rename all .sh files to .shell and delete their second line, but a tiny typo—omitting the $ before a variable—caused every .sh file to disappear, illustrating the importance of careful command‑line scripting.
In a live‑stream the presenter attempted to write a Bash script that (1) finds every file ending with .sh in the current directory and sub‑directories, (2) renames each to have a .shell extension, and (3) removes the second line of each file.
Correct script
#!/bin/bash
ALL_SH_FILE=$(find . -type f -name "*.sh")
for file in ${ALL_SH_FILE[*]}
do
filename=$(echo $file | awk -F'.sh' '{print $1}')
new_filename="${filename}.shell"
mv "$file" "$new_filename"
sed -i '2d' "$new_filename"
doneStep‑by‑step explanation
Find .sh files : the find . -type f -name "*.sh" command lists all regular files whose names end with .sh.
Rename the extension : for each file, the script extracts the name without the .sh suffix (using either Bash parameter expansion ${file%.sh*} or awk -F'.sh') and appends .shell, then moves the file with mv "$file" "$new_filename".
Delete the second line : sed -i '2d' "$new_filename" edits the file in place, removing its second line.
The bug that caused the disaster
During the live demo the presenter mistakenly wrote the rename command as:
mv "$file" "new_filename"Because the leading $ was omitted, the literal string new_filename was used as the destination for every .sh file, overwriting the same file repeatedly and effectively deleting all original scripts.
Outcome and lesson
The missing $ turned the intended batch rename into a destructive operation, demonstrating how a single character error in shell commands can have catastrophic results. The author stresses the need for careful review, especially when using powerful commands like rm, mv, and cp on Linux.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
