Modern technology gives us many things.

A Simple Guide With Examples

0


Variables are used for storing values of different types during program execution. There are two types of variables in Bash scripting: global and local.

Global variables can be used by all Bash scripts on your system, while local variables can only be used within the script (or shell) in which they’re defined.

Global variables are generally provided on the system by default and are mainly environment and configuration variables. Local variables, on the other hand, are user-defined and have arbitrary uses.

Bash Local Variables

To create a variable, you need to assign a value to your variable name. Bash is an untyped language, so you don’t have to indicate a data type when defining your variables.

var1=Hello

Bash also allows multiple assignments on a single line:

a=6 b=8 c=9

Just like many other programming languages, Bash uses the assignment operator = to assign values to variables. It’s important to note that there shouldn’t be any spaces on either side of the assignment operator. Otherwise, you’ll get a compilation error.

Related: What Does “Bash” Mean in Linux?

Another key point to note: Bash doesn’t allow you to define a variable first and then assign a value to it later. You must assign a value to the variable at creation.

var2 # compilation error 
var2=World

Sometimes, you may need to assign a string that has a space in it to your variable. In such a case, enclose the string in quotes.

MAKEUSEOF VIDEO OF THE DAY
# var3=Hello World 
# above assignment doesn't work
var3='Hello World'

Notice the use of single quotes. These quotes are also called “strong quotes” because they assign the value precisely as it’s written without regard to any special characters.

In the example above, you could have also used double quotes (“weak quotes”), though this doesn’t mean they can always be used interchangeably. This is because double quotes will substitute special characters (such as those with $), instead of interpreting them literally.

See the example below:

var4=89
echo "The number is $var4"
echo 'The number is $var4'
# First output prints: 89
# Second output prints leaves "$var4" as it is

If you want to assign a command-line output to your variable, use backquotes (). They’ll treat the string enclosed in them as a terminal command and return its result.

var5="You are currently working in this directory: `pwd`"

Parameter Expansion in Bash

Parameter Expansion simply refers to accessing the value of a variable. In its simplest form, it uses the special character $ followed by the variable name (with no spaces in between):

var6=Jack
echo My name is $var6

You can also use the syntax ${variableName} to access a variable’s value. This form is more suitable when confusion surrounding the variable name may arise.

m=Med
echo Med stands for ${m}ical

If you leave out the curly brackets, ${m}ical will be interpreted as a compound variable (that doesn’t exist). This use of curly brackets with variables is known as “substitution”.

Global Variables

As mentioned earlier, your Linux system has some built-in variables that can be accessed across all of your scripts (or shells). These variables are accessed using the same syntax as local variables.

Related: How to Create and Execute Bash Scripts in Linux

Most of these variables are in BLOCK letters. However, some are single characters that aren’t even alphanumeric characters.

Here are some common useful global variables:

HOME: Provides the user’s home directory

SHELL: Provides the type of shell you’re using (e.g Bash, csh..etc)

?: Provides the exit status of the previous command

To get a list of global variables on your system, run the printenv (or env) command:

$ printenv 

Loops in Bash Scripting

Now you know what variables are, how to assign them, and how to perform basic Bash logic using them.

Loops enable you to iterate through multiple statements. Bash accommodates for loops and while loops with a simple syntax for all of your looping needs.

If you’re mastering the art of Bash development, for loops ought to be next up on your list.


Woman explaining code to a man
How to Use the for Loop in a Linux Bash Shell Script

Why run the same code again and again in your shell script when you can have a for loop do it for you?

Read Next


About The Author



Source link

Leave A Reply

Your email address will not be published.