facebook youtube pinterest twitter reddit whatsapp instagram

Working With Variables In Bash

A variable is a value that can change depending on conditions or data passed to the program. To work with the variable in bash, here are what you need to know:

  • A variable name cannot start with a number, and cannot contain spaces.
  • It can be alphanumeric
  • It can start with an underscore

To declare a variable, type the name you want and set its value using the equals sign (=), make sure there is no space between the variable name and the equal sign or between the equals sign and the value, otherwise it would throw an error.

For example:

#!/bin/bash
b="How are You"
c=15

To use this variable, you call it with a dollar sign in front of their name.

E.g:

#!/bin/bash
b="How are You"
c=15

echo $b
echo $c

This is my output:

user@server:~/bin$ hw4.sh
How are You
15

These variables can also work inside other variable or strings, just make sure they are double quoted, e.g:

#!/bin/bash
b="How are You"
c=15

echo "b! You are $c old."

Output:

user@server:~/bin$ hw4.sh

How are You! You are 15 old.

Also, you can give variable a special attribute, e.g declaring it as an integer or as a read-only, meaning it can't be modified later arithmetically or with stream manipulation.

The one I find interesting is converting strings to a lowercase or uppercase:

  • declare -i b=700 #b is an integer
  • declare -r e=300 #e is read-only
  • declare -l j="Lowercase" #j would be converted to lowercase
  • declare -u k="Uppercase" #k would be converted to UPPERCASE

Major Built-in Variable

  • echo $PWD - Returns the current directory
  • echo $HOME - User home directory
  • echo $MACHTYPE - Returns the machine type, handy when working on a different platform
  • echo $HOSTNAME - Returns system name

 

Related Post(s)

  • sed (Stream Editor) In Bash Scripting

    Another powerful filter in GNU/Linux is sed (stream editor), which is a program for performing basic editing tasks on the output of another program or on a file. sed or stream editor can be used to p

  • awk In Bash Scripting

    Up until now, we have covered a couple of text processing, from sed to grep to sort, and now we have the big daddy, which is awk. awk (which stands for Aho, Weinberger, and Kernighan - its creators)

  • (Bash Script) Automate Multiple Classicpress & Wordpress Installation + [Caching and SSL]

    If you are tired of using control panels (I am because they are mostly not secure, and most comes loaded with bundles of useless stacks), and want to do everything on the server level, I wrote a menu

  • Returning Values From Functions in (Bash)

    In the guide function in bash, we dive deep in bash functions and we also pass some information into the functions to carry out a certain operation. In this guide, I'll show you how to get informatio

  • Functions in Bash

    Functions are modular building blocks for creating powerful and modular scripts, using function makes it easy to isolate a code since it would be in a single building block, which would be isolated f

  • grep (Regular Expression) In Bash Scripting

    We recently discussed filters in bash scripting, in this guide we would look more into the practical usage of using a filter program, and an example of such a program is grep. grep, in its simplest f