DEV Community

Cover image for Master Shell Scripting – Zero to Interview Ready!πŸ”₯

Master Shell Scripting – Zero to Interview Ready!πŸ”₯

Harshit Rawat on February 10, 2025

Introduction Hey Everyone, Good to see you back! For us programmers, diving into the world of Bash scripting is really important. It's a...
Collapse
 
rajeshroyal profile image
Rajesh Royal β€’

@harshit_rwt there is an error in the firstscript.sh file, It will not create 5 files (at least for me) instead it will create only 1 file with the name file_{1..5}.txt.

The below code works fine:

# Correct way to create the files:
for i in $(seq 1 5); do  # Use seq to generate the numbers
    file_name="$dir_name/file_$i.txt"
    touch "$file_name"
    if [ ! -f "$file_name" ]; then
        echo "Error: Failed to create file '$file_name'."
    fi
done
Enter fullscreen mode Exit fullscreen mode

Nice article and good reading.

Collapse
 
harshit_rwt profile image
Harshit Rawat β€’

Hi @rajeshroyal,
Thanks for your feedback! I appreciate the suggestion. The approach I used {1..5} does create five separate files in Bash, since it expands before execution, generating unique filenames (file_1.txt, file_2.txt, etc.).

I see why you might be getting file_{1..5}.txt , this can happen if the script is run in sh instead of bash, as sh doesn’t support {1..5} expansion. Your (seq 1 5) approach is a great alternative for broader shell compatibility, though {1..5} is slightly more efficient in pure Bash.

Really appreciate the discussion, always great to learn different approaches! 😊

Collapse
 
rajeshroyal profile image
Rajesh Royal β€’

Thanks @harshit_rwt

Collapse
 
shubh_shekhar_dbeb5c2df19 profile image
shubh shekhar β€’ β€’ Edited

"This is what i am looking for πŸ”₯πŸ”₯"

Collapse
 
ash_williams_eda2a579e499 profile image
Ash Williams β€’

Very informative

Collapse
 
ibikunle_0d4104dc76bb08b0 profile image
Ibikunle β€’

This is a great tutorial on bash script.
Thank you

Collapse
 
harshit_rwt profile image
Harshit Rawat β€’

Thankyou for such a positive feedback,
Glad you liked it!

Collapse
 
mahadev143143 profile image
mahadev143143 β€’

Nice

Collapse
 
ramkumar-m-n profile image
Ramkumar M N β€’

Informative, Thank you.

Collapse
 
mikem164 profile image
MikeM164 β€’

Great tutorial on bash scripting. Thanks

Collapse
 
madhurima_rawat profile image
Madhurima Rawat β€’

Such a great article, so thorough and detailed. Thanks for this, Great πŸ‘ work indeed.

Collapse
 
harshit_rwt profile image
Harshit Rawat β€’

Thank you for your kind words! Really happy that you enjoyed it. Your appreciation means a lot! ✨

Collapse
 
d_c_b8165ca6e7be316dc8ae6 profile image
D C β€’ β€’ Edited

Because it's basics, I think you need to mention how to save and exit the vim after entering the content. Like :wq at the beginning.