Bash scripting quirks & safety tips

Recently I was conversing with certain companions about Bash and I understood that, despite the fact that I’ve been utilizing Bash for over 10 years now there are as yet a couple of essential characteristics about it that are not absolutely evident to me. So as normal I suspected I’d compose a blog entry.

We’ll cover

some slam fundamentals (“how would you compose a for circle”)

peculiar things (“consistently quote your slam factors”)

what’s more, slam scripting wellbeing tips (“consistently utilize set – u”)

On the off chance that you compose shell contents and you don’t peruse whatever else in this post, you should realize that there is a shell content linter called shellcheck. Use it to make your shell contents better!

We ‘re going to discuss slam like it’s a programming language, since, well, it is. The objective of this post truly isn’t to go into subtleties of slam programming. I don’t do entangled programming in slam and don’t generally plan to figure out how to. In any case, subsequent to considering it a piece today, I believe it’s valuable to expressly record a portion of the rudiments of the slam programming language. What’s more, a few things about the slam programming dialects are very unique in relation to other programming dialects I use!

I truly thought I knew this stuff as of now yet I took in a few things by composing this post so perhaps you will as well.

Variable task

In slam variable task resembles this:


also, you reference factors with $VARIABLE. It’s significant that you don’t put spaces around the = sign – VARIABLE= 2, VARIABLE = 2, and VARIABLE =2 are not language structure blunders, however will all do distinctive undesirable things (like attempt to run a program called 2 a situation VARIABLE set to the vacant string).

Slam factors don’t should be all-tops yet they generally are.

Most slam factors you’ll utilize are strings. There are likewise some cluster factors in slam however I don’t generally comprehend those.

Citing your factors with ${}

Now and then I have a variable containing a string like file.txt and I need to do like

mv $MYVAR $MYVAR__bak # wrong!

This code as is won’t work! It will rather search for the variable MYVAR__bak which is anything but a genuine variable.

To get around this, all you have to know is that ${MYVAR} does likewise as $MYVAR. So we can run

mv $MYVAR ${MYVAR}__bak # right!

worldwide, nearby and condition factors

Next, Bash has 3 sorts of factors. The thoughtful I ordinarily consider first (and presumably utilize the regularly) are condition factors.

Each procedure on Linux bash for loop really has condition factors (you can run env to perceive what factors are at present set), however in Bash they’re substantially more effectively open. To see the earth variable called MYVAR you can run.

reverberation “$MYVAR”

To set a situation variable, you have to utilize the fare watchword:

send out MYVAR=2

At the point when you set a situation variable, all youngster procedures will see that condition variable. So on the off chance that you run send out MYVAR=2; python, the python program will have MYVAR set to 2.

The following sort of factor is the worldwide variable. You dole out these simply like we depicted up above.


They act like worldwide factors in some other programming language.

There are additionally nearby factors, which are perused to just exist inside a slam work. I fundamentally never use works so (not at all like in truly every other programming language I write in) I have never utilized neighborhood factors.

for circles

Here’s the way I compose for circles in slam. This circle prints the numbers from 1 to 10.

for I in ‘seq 1 10’ # you can utilize {1..10} rather than ‘seq 1 10’


reverberation “$i”


On the off chance that you need to compose this circle on one line it would appear that this:

for I in ‘seq 1 10′; do resound $i; done

I locate this difficult to recollect (how are you expected to recall that there’s a semicolon after seq 1 10 however none after do?) so I don’t attempt to recollect that.

You can likewise compose while circles yet I never do that.

The cool thing about this is you can repeat over the yield of another direction. seq 1 10 prints the numbers from 1 to 10 (one for every line), and this for circle is simply taking that yield and repeating over it. I utilize this a decent lot.

You can add order yield with either backticks or $().


# or