Showing posts with label variables. Show all posts
Showing posts with label variables. Show all posts

Friday, February 1, 2013

Variables in Bash script

Variables in Bash script language have following properties:
  • They haven't data type
  • There is no need to declare variable
So, let's see how they works on simple "Hello World" Example:
#!/bin/bash
Var1 = "Hello World!"
echo $Var1
This simple example will output sentence Hello World!

 Interesting with $ sign in Bash is, if you put something in brackets after $ this will be interpreted as interpreter command.
#!/bin/bash
Var1= $(ls -la)
echo $Var1
This simple example will put output from ls -la command in $Var1 variable and then print content on screen.

Important thing about variables is that you can use local variables in functions, using keyword local.

local Var1="Local variable"

Builtin variables

 

 Bash have Built-in variables, some of them are:
  • $BASH - Path to the Bash binary
  • $BASHPID - Process ID of Bash 
  • $BASH_VERSION - Version of Bash
  • $HOME - Home directory of user
  • $MACHTYPE - Machine type
  • $PATH - Path to binaries
  • $PWD - Working directory
  • $SECONDS - number of second while script is running
  • $UID - User ID number
  • $0,$1 ... - Command line arguments
  • $# - Number of command line arguments
And many more...