Basic Commands
and Simple Shell Scripts94
We can run the script, obtaining the same output as before:
[root@rhel-instance
]# ./hello.shhello world!To
have more clarity, when using the value of the variable, we will put the name
of it between curly braces, { and }’, and take this as a good practice.
The previous script will look as follows:
#!/bin/bash
PLACE=''world''
echo 'hello ${PLACE}!''
We now know how to create a basic script, but we may want to have greater control over it by using
some programmatic capabilities, starting with loops. Let’s go for it!
for loopsWhat if we want to run the same command over a list of places That’s what a for
loop is used for. It can help
iterate over a set of elements, such as a list or a counter, for example.
The for loop syntax is as follows for To specify the iteration do To specify the
action done To close the loopWe can define a space-separated list to try it and iterate through it with our first for loop:
#!/bin/bash
PLACES_LIST="Madrid Boston Singapore World"
for PLACE in ${PLACES_LIST}; do echo "hello ${PLACE}!"
done
Let’s run it. The output will look as follows:
Share with your friends: