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.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

echo<br />

if [ TRUE ]<br />

then<br />

alias rr="ls −l"<br />

echo "Trying aliased \"rr\" within if/then statement:"<br />

rr /usr/X11R6/bin/mk* #* Error message results!<br />

# Aliases not expanded within compound statements.<br />

echo "However, previously expanded alias still recognized:"<br />

ll /usr/X11R6/bin/mk*<br />

fi<br />

echo<br />

count=0<br />

while [ $count −lt 3 ]<br />

do<br />

alias rrr="ls −l"<br />

echo "Trying aliased \"rrr\" within \"while\" loop:"<br />

rrr /usr/X11R6/bin/mk*<br />

let count+=1<br />

done<br />

echo; echo<br />

#* Alias will not expand here either.<br />

# alias.sh: line 57: rrr: command not found<br />

alias xyz='cat $0' # Script lists itself.<br />

# Note strong quotes.<br />

xyz<br />

# This seems to work,<br />

#+ although the Bash documentation suggests that it shouldn't.<br />

#<br />

# However, as Steve Jacobson points out,<br />

#+ the "$0" parameter expands immediately upon declaration of the alias.<br />

exit 0<br />

The unalias command removes a previously set alias.<br />

Example 24−2. unalias: Setting and unsetting an alias<br />

#!/bin/bash<br />

shopt −s expand_aliases # Enables alias expansion.<br />

alias llm='ls −al | more'<br />

llm<br />

echo<br />

unalias llm<br />

# Unset alias.<br />

llm<br />

# Error message results, since 'llm' no longer recognized.<br />

exit 0<br />

bash$ ./unalias.sh<br />

total 6<br />

drwxrwxr−x 2 bozo bozo 3072 Feb 6 14:04 .<br />

Chapter 24. Aliases 287

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

Saved successfully!

Ooh no, something went wrong!