media file explorer download upload index folder image licenses menu widget Play Pause profile-settings more dots-two-horizontal dots-two-vertical more-vertical pending google-plus hangouts facebook instagram whatsapp spotify telegram twitter vine renren rss youtube twitch vimeo flickr dribble behance deviantart 500px steam github soundcloud skype reddit linkedin lastfm delicious stackoverflow pinterest xing flattr foursquare yelp World

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:

b="How are You"

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


b="How are You"

echo $b
echo $c

This is my output:

How are You

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

b="How are You"

echo "b! You are $c old."



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