Learn Bash: Tutorial 004
Categories:
[Technology]
Tags:
[open-source],
[linux],
[bash]
Welcome back to our Bash tutorial, in this post we will get deeper than the last. Head back and read those first if you are new to Bash scripting. Otherwise we can move forward and talk about more awesome Bash tools to make our computing life easier.
As said in the lasts post if you are reading this and you know bash please help by filling in any gaps in thought via the comment section. The goal is to keep these about one thousand words long and there may be parts I brush over. So please expand anything that will help a new bash user.
I will add an edit to the main content and link to each comment I feel points out a useful tip or expands upon a section in more detail. Those comments will also be up-voted by myself to keep them near the top of the trending list.
Also, thanks to everyone who has added great points in the past three posts. You all are making this series as in depth as it can be. I kind of wish I had this when first learning Bash! The more we work together the better off new users will be and that is what it is all about.
Variables
There are few parameters in Bash able to hold a value. Variables are one of those few that can do so. Variables are used in programming languages to hold a value from the start or act as an empty box that the program can fill with data.
The same goes for Bash scripting. I don't consider Bash a programming language but it operates in similar fashion. Computers all operate with the same logic so any time we need to tell it to do something that logic needs to apply. Let's look at what using a variable looks like in a short, simple script.
#!/bin/bash
blog=personal
echo "Hello, ${blog}!"
Here is what the above script will do once executed. blog
is the variable and we assign that value personal
to said variable. On the third line of the script we tell Bash to echo
or write what follows to our output. What we see on the screen is "Hello, personal!" because Bash knows to replace ${blog}
with "personal".
Pretty cool and helpful when writing long scripts that needs to reuse pieces of data. Notice I said data and not "text". This is because we can store much more than plain text in a variable. Numbers, symbols, and even commands can store inside a variable. Let's change the script above so that echo
is the stored data with the same result.
#!/bin/bash
exe=echo
"${exe}" 'Hello, universe!'
What we see above is the command echo
stored in the variable. When the script reaches that section during execution it swaps our variable text for the data stored, in this case it is echo
. Then the script will finish and output the same line of text as above, "Hello, universe!".
Another thing to keep in mind when writing a bash script with variables is the use of ' '
and " "
. These act is a similar manner for much of our day to day but will change the outcome of a Bash script. If we take our example above and change "${exe}"
to '${exe}'
we do not get the some result.
We must use the double quotes when referring to a variable else the data stored in the variable will not be used. "${exe}"
will use the data echo
as we desire. However, if we use single quotes instead the stored data does not get used. What the script interprets the command as is exe "Hello. universe!"
.
In these two examples we use the syntax of ${*variable-name*}
but there is a shorter way to achieve the same end. This is by dropping the curled braces and using $*variable-name*
. Both will function the same not effecting how our code runs but sometimes the curled braces are important.
One such example is ${jr}swab verses $jrswab. It the first example the "jr" inside the { }
gets replace by the stored data in the "jr" variable. In the second example the script would look for the variable jrswab
. Let's break down this idea.
#!/bin/bash
jr="Cotton"
jrswab=blogger
echo "${jr}Swab"
echo "$jrswab"
What we get upon running this script are two lines with similar variables but different outcomes. The first echo
will show us "CottonSwab" while the second will display "blogger". This is a simplified example to illustrate the point that in some occasions the braces matter. Most real word scripts do not use the braces unless there is a strict reason to do so.
What if we want to have a space in our variable for the sake of formatting when Bash outputs the information? To do so, we need to use more quotes. Here is an example:
#!/bin/bash
jr='Cotton '
echo "${jr}Swab"
The result of the script above will be Cotton Swab
. Most of the time we should use the single quotation marks when adding strings of text or commands to a variable. However, with variable expansion we must use double quote. You should recall when we talked about file name expansion. This is the same idea but using variables.
#!/bin/bash
jr="Cotton *"
echo "${jr}Swab"
If we were to write the above script using single quotes instead of double quotes, we are then prone to executing cotton *
as a filename expansion command. We will get more into variable expansion in a future post but I wanted to show the importance of syntax when using variables in this lesson.
Finally, there are some pre-defined variables in Bash. One being HOME
, this variable will always default the home directory of the user running the file. This is helpful when writing a script to be used across devices or user accounts.
Ways to support the blog.
If you are an email kind of nerd you can sign up for mine here. You can donate to this site from my Liberapay account if you so choose. If you want a more passive way to support this site, use this link when shopping on Amazon; it kicks some of Amazon's profit to me at no extra cost to you.