DEV Community

Arthur Nunes
Arthur Nunes

Posted on • Edited on

First steps with ShellScripting

As most of you - don't - know, I've been a loyal Linux user for pretty much a year by now and I love it, it helps me stay productive and I've always been an open-source lover.
Also, I have the not-so-healthy habit of formatting my computer almost weekly, with Windows that was a pain, but in Linux I can write a script to do everything I need for me. I do not recommend you to do that to your PC that frequently, but I can't help myself. So, with the goal of saving me some hours every week, I proceeded to write it, in this post I'll explain what I learned in my first week of studying.

PS: I only know JavaScript, and my first experiences with programming were all in JavaScript, so JS will be the referential.

1. Spacing and quoting are very important

I was very surprised when I discovered that sometimes spacing in between brackets in conditional statements mattered, and that I had to quote a lot more than expected to make bash read my strings with spaces as one, even though they were declared quoted. Here's an example:

array=(
  item0
  "item 1"
)

for each in ${array[@]}; do
  echo $each
done
Enter fullscreen mode Exit fullscreen mode

Output:

item0
item
1
Enter fullscreen mode Exit fullscreen mode

But, if we change ${array[@]} to "${array[@]}", the output will be:

item0
item 1
Enter fullscreen mode Exit fullscreen mode

As I would have expected because of my JS baggage.

In JavaScript you can pretty much skip a lot of "code formalities" like that, you can skip semi-colons at the end of operations, you can space practically how you want the parentheses of your if statements and more.
The answer to this question explains it well enough if you want to read it.
JavaScript's syntax is a lot more flexible than bash's.

2. Content disparity

We all know that JavaScript is well-established right now, and that means that there is a lot of content in that language, from tutorials to questions and answers in Stack Overflow.
That, at first, seems like a great thing, and it almost always is, but it wasn't uncommon for me to waste 15+ minutes to find an answer to a simple question(that is also because of the infinity of ways to do things in JavaScript) and it was very frustrating.

In contrast, for bash I found a lot less content, but because of the objectivity of the language, I was able to find answers quicker, I recurrently found what I was looking for in the first or second link that I opened. A great example of this is the page that I linked in the first topic, I found it in less than 5 minutes, even though I was not using very specific terms to google it.

3. Conclusion

I had a great time learning to write ShellScript and I feel revigorated after writing different code for a while, sometimes a simple change for a week or two can help you a lot!

Thank you for reading!

You can find me on Twitter and on GitHub (The script I talked about is there).

This post is a repost of an old article of mine written in 2020-08-26.

Top comments (0)