27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9. Variables Revisited<br />

Used properly, variables can add power and flexibility to scripts. This requires learning their subtleties and<br />

nuances.<br />

9.1. Internal Variables<br />

Builtin variables<br />

variables affecting bash script behavior<br />

$BASH<br />

the path to the Bash binary itself<br />

bash$ echo $BASH<br />

/bin/bash<br />

$BASH_ENV<br />

an environmental variable pointing to a Bash startup file to be read when a script is invoked<br />

$BASH_VERSINFO[n]<br />

a 6−element array containing version information about the installed release of Bash. This is similar<br />

to $BASH_VERSION, below, but a bit more detailed.<br />

# Bash version info:<br />

for n in 0 1 2 3 4 5<br />

do<br />

echo "BASH_VERSINFO[$n] = ${BASH_VERSINFO[$n]}"<br />

done<br />

# BASH_VERSINFO[0] = 2 # Major version no.<br />

# BASH_VERSINFO[1] = 05 # Minor version no.<br />

# BASH_VERSINFO[2] = 8 # Patch level.<br />

# BASH_VERSINFO[3] = 1 # Build version.<br />

# BASH_VERSINFO[4] = release # Release status.<br />

# BASH_VERSINFO[5] = i386−redhat−linux−gnu # Architecture<br />

# (same as $MACHTYPE).<br />

$BASH_VERSION<br />

the version of Bash installed on the system<br />

bash$ echo $BASH_VERSION<br />

2.04.12(1)−release<br />

tcsh% echo $BASH_VERSION<br />

BASH_VERSION: Undefined variable.<br />

Checking $BASH_VERSION is a good method of determining which shell is running. $SHELL does<br />

not necessarily give the correct answer.<br />

$DIRSTACK<br />

the top value in the directory stack (affected by pushd and popd)<br />

This builtin variable corresponds to the dirs command, however dirs shows the entire contents of the<br />

directory stack.<br />

Chapter 9. Variables Revisited 64

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!