Bash Scripting Tutorial for Beginners - LinuxConfig.org

Most likely, your are at the moment sitting in front of your computer, have a terminal window opened and wondering: "What should I do with this thing?" Well, the terminal window in front of you contains shell, and shell allows you by use of commands to interact with your computer, hence retrieve or store data, process information and various other simple or even extremely complex tasks.
This is a companion discussion topic for the original entry at https://linuxconfig.org/bash-scripting-tutorial-for-beginners
1 Like

Disqus import:

  1. Htet Htet Htun • 9 days ago

Excellent tutorial.thanks a lot

  1. Billy Bobthornton • a month ago

Excellent tutorial.

For a guy who knows about about scripting you have done me the world of good.

Thanks so much. God Bless.

  1. trilogy1000 • 2 months ago

I agree with the previous comments, very well written and explained.
If you are looking to expand it, could I suggest working through file handling - copying, moving, naming, renaming, removing/extracting file extensions etc.
We have a very heavily scripted workflow here using Applescript and it’s virtually all to do with file handling. I’d love to have a go at rewriting some of it into shell scripts.

  1. gary • 5 months ago

I can’t get over how well you presented bash scripting to new users. Simply a remarkable write-up! You started off at the foundations of the CLI and built up to more advanced concepts very comfortably. I’ve tried to do the same for some users IRL but I’m just going to share this instead:)

  1. Avi gary • 3 months ago

I join Gary. Excellent tutorial. Thanks a lot!

Thank you for an incredibly motivating feedback. I had a pleasure to teach Bash Scripting to beginner students at the local university for more than six years. Perhaps this gave me a little bit of insight on how to present the material and avoid hurdles at the same time.

Writing tutorials for beginners is much harder than writing advanced topic tutorials for advanced users. That much is clear. In any case, I still consider the work on this tutorial as “ongoing” thus as version 1.0.0. I will wait for any other additional feedback from readers, and if needed, I will update it accordingly.

Please forgive the guide. In the link below, I will download the tutorial video
link

Thank you very much for the stuff. this is the best tutorial for bash which i found in internet. I read it for 3 days carefully while working in office also bookmarked the site. (Isuru From SRI LANKA)

Absolutely amazing tutorial, thanks for your dedication and I like the exercise part it’s quite motivational.

I just got a job and my superior wanted me to learn Bash Scripting. i have never done this type of work before but this tutorial was extremely helpful and clear and my superior is impressed with what i learned. Thank you!

Thank you very much for this excellent tutorial.

Wow! what an excellent tutorial. So easy to understand the whole bash scripting just with one single web page

1 Like

Kudos to the creator of this tutorial. I have reached the section for the backup and noticed that when I ran it with the original code, i did get a message that the backup failed but did get the backup file. It looks like the comparison for the source files and the archived files did not agree with the same number. Did anyone else see this? I have run the backup script in two different linux distros I have and never get the same number for source files and archived files. The latter number is always a higher number. So to avoid the nagging message about the backup failing, I modified the script to include three cases for the source and archived files comparison:

  1. source files -le archived files
  2. source files -eq archived files
  3. source files -ge archived files
    Depending on what the comparison result is, I output a message for the output file and thus avoid the failure message. I have not found an issue with the directories comparison.

Cheers! :sunglasses:
moonshiner45

Hi Moonshiner45,

Welcome to our forums.

The tar command mentioned in the tutorial can fail for various reasons - the most trivial would be that the user you run it with does not have read permissions to every file under the source directory - for example, of there is a file owned by root in my home directory, and I run tar as a normal user, I’ll get an error on that file.

sandmann,
Thank you for the clarification, it makes perfect sense! I realize that what you mention did not cross my mind before I added my post.

Thanks for great tutorial! Your explanations are easy to follow.
I needed a refresher on the scripting syntax, but I also learned new useful commands (ncal -w is great!).

Hello, learning bash and here from google search. I think the formatting might be broken on the webpage and it took me ages to clue in that it might be the problem. I followed the tutorial until I had trouble with the backup script in the Functions section and then got stuck on the same with the Conditionals section.

I guessed the \$1s in the Function section weren’t meant to be escaped, the forum’s doing the opposite to me, so I guess that’s where that’s from.

Brilliantly !!! :+1::heartpulse:

Hi Lubos,
thank you for the excellent tutorial! But I have one question regarding it: what is $1 (started in function part) and why it is not explained there? :slight_smile:
All the best, Marek D. Koter

Hi Marek,

$1 is positional parameter. You can assign value to a positional parameter via command line argument(s). This topic is explained later. Please check the “Positional Parameters” section for more info.

Lubos

Hi Sandmann,

Why would this result in the tar file showing as having more files than the home directory? If there was a file owned by root which results in an error on that file, then the tar file would not include this. So the arch_files variable should be lower than the src_files variable. Or is my understanding wrong?

I am having the same problem as moonshiner45 where arch_files is always 4 greater than src_files. Any advice would be appreciated.

I am adding some script to try to identify those four files but my grep -xvFf isn’t giving me those four as I would expect. Hopefully when I identify the four files I might understand why.

Thanks

Hi GreenPoint,

Welcome to our forums.

The cause of higher number of arch_files related to src_files may be symbolic links. I have created a test directory, and a few files in it:

$ ll test
drwxrwxr-x  3 sandmann sandmann  4096 jan    9 16:46 ./
drwxrwxrwt 22 root     root     12288 jan    9 16:48 ../
drwxrwxr-x  2 sandmann sandmann  4096 jan    9 16:47 dir/
-rw-rw-r--  1 sandmann sandmann     0 jan    9 16:43 file1
-rw-rw-r--  1 sandmann sandmann     0 jan    9 16:43 file2
-rw-rw-r--  1 sandmann sandmann     0 jan    9 16:43 .file3
$ ll test/dir/
drwxrwxr-x 2 sandmann sandmann 4096 jan    9 16:47 ./
drwxrwxr-x 3 sandmann sandmann 4096 jan    9 16:46 ../
-rw-rw-r-- 1 sandmann sandmann    0 jan    9 16:44 file4
lrwxrwxrwx 1 sandmann sandmann    5 jan    9 16:47 file-link -> file1

Notice the last line, which shows a symbolic link to file1, which is in the test directory. Now if I run the find command from the script, I’ll get the result of four:

$ find test/ -type f | wc -l
4

That’s because we gave find the argument of -f, which means search for files, and symbolic links does not match (that would be -l).

I can now create the tar archive that will contain all of them:

$ tar -czf test.tar.gz test

And calculate the number of “items” that are not directories:

$ tar -tzf test.tar.gz | grep -v /$ | wc -l
5

Voila, I get a higher number on arch_files, since the src_files calculation does not include symbolic links.

Having trouble with the let command. What am I doing wrong?

let b=4*($a-1)
-bash: syntax error near unexpected token `('