Help with understanding of alias


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with understanding of alias
# 1  
Old 05-04-2012
Help with understanding of alias

Hi, I saw the following explanation about alias in bash from gnu website, but I didn't get the meaning:
Bash always reads at least one complete line of input before executing any of the commands on that line.
Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition
appearing on the same line as another command does not take effect until the next line of input is read. The
commands following the alias definition on that line are not affected by the new alias. This behavior is also an
issue when functions are executed. Aliases are expanded when a function definition is read, not when the
function is executed, because a function definition is itself a compound command. As a consequence, aliases
defined in a function are not available until after that function is executed. To be safe, always put alias
definitions on a separate line, and do not use alias in compound commands.

especially the part related with function, can anybody help explain in short sentences cause I am not a English speaker.
Great Thanks.
Roy987
# 2  
Old 05-04-2012
Here is an example:

Code:
# alias et='echo test'
# function doit {
> et
> }
# doit
test
# alias et='echo done'
# doit
test
# unalias et
# doit
test

See how changing the "et" alias makes no difference to the output. This is because et has already been expanded when the function was first defined.
These 2 Users Gave Thanks to Chubler_XL For This Post:
# 3  
Old 05-04-2012
The shell reads small chunks of the script which might be a single line from the file or might be a group of lines (a while or for loop is read all at once). As the shell reads these chunks the current alias definitions are applied to that chunk.

The same goes for functions. Each function in the script is read as a group of lines, and the aliases that are listed in the function are replaced as the function is read.

Consider this small example

Code:
function print_stuff
{
    p "$*"
}

alias p="echo \"$(hostname):\""   # alias p is not defined when function read

print_stuff "sample message"

It is common for a script to define all functions at the top of the script and to have a section just below for the initialisation of variables. A programmer might also think to define aliases in that initialisation section, but that's what the description is trying to point out as being wrong.

In the code above, the assumption is that because p is aliased before the function is executed, it will have the alias value at the time it is called. This is not the case.

As the shell reads the script, it reads in the function and because the alias p is not set when the function is read, the p in the function is not expanded. When the function is executed, an error message (no such command p, or something like that) is generated.

If the alias is defined before the function is read by the shell, then when the function is read p is expanded and all works as desired. The following is an example of this:

Code:
alias p="echo \"$(hostname):\""   #alias p will exist when function is read

function print_stuff
{
    p "$*"
}

p "sample message"

The same applies to a loop, but the "trap" is a bit different:

Code:
while read buffer
do
    if [[ $buffer == *"foo"* ]]
    then
       alias process_cmd="interpret_foo"
    else
       alias process_cmd="interpret_other"
    fi

    interpret_command  "$buffer"
done <some-file

This is an unrealistic example, but it illustrates what the text you posted is saying. The process_cmd alias will not be set until the while loop is executed, which means the command interpret_command "$buffer" will not be expanded.

I hope this helps. It is a difficult concept to put into words.
This User Gave Thanks to agama For This Post:
# 4  
Old 05-04-2012
Thanks a lot to agama, Chubler_XL, I can understand now.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need your help in understanding this

Hi, I found this in a script and I would like to know how this works Code is here: # var1=PART1_PART2 # var2=${var1##*_} # echo $var2 PART2 I'm wondering how ##* makes the Shell to understand to pick up the last value from the given. (2 Replies)
Discussion started by: sathyaonnuix
2 Replies

2. UNIX for Dummies Questions & Answers

Create alias files (not alias commands)

If one: $ find -name 'some expression' -type f > newfile and then subsequently wants to create an alias file from each pathname the find command retrieved and the > placed within 'newfile', how would one do this? Ideally, the newly created alias files would all be in one directory. I am... (3 Replies)
Discussion started by: Alexander4444
3 Replies

3. UNIX for Dummies Questions & Answers

understanding {%/*}/

Hi Gurus: I am trying to understand the following line of code.I did enough of googling to understand but no luck.Please help me understand the follow chunk of code: X=$0 MOD=${X%/*}/env.ksh X is the current script from which I am trying to execute. Say if X=test.ksh $MOD is echoing :... (3 Replies)
Discussion started by: vemana
3 Replies

4. UNIX for Dummies Questions & Answers

using alias

I need to login to one server and then switch the user and set a number alias. But i cant modify the .profile file. I have one script avi1.sh $ more avi.sh sudo su - bil sh avi1.sh and in home directory of bil i have avi1.sh that says $ more avi1.sh alias l='ls -ltr' alias b='cd... (7 Replies)
Discussion started by: blackeyed
7 Replies

5. UNIX for Dummies Questions & Answers

alias

Hi, I have created alias in UNIX environment (ie in .cshrc file). But when i use the alias in the UNIX script, getting an error message as that is not found. But when i use that alias, outside the script it is working fine. Let me know if you need more details For example: alias cderror... (6 Replies)
Discussion started by: Naveen_5960
6 Replies

6. UNIX for Dummies Questions & Answers

Help with Alias

Hi, I have a software package consisting of a group of BASH shell scripts. There is a master script which calls a series of subscripts. To run the software users must open the master script, modify and save it, and then run it. To open the script you can either double-click on it, or you can... (2 Replies)
Discussion started by: msb65
2 Replies

7. Shell Programming and Scripting

need help understanding mv

I just started shell coding and I'm a bit confused on how 'mv' works can someone explain to me how it works and if i did this correctly. Thanks. echo "Enter Name of the first file:" read file1 #echo $file1 if ; then echo "Sorry, file does not exist." exit 1 ... (16 Replies)
Discussion started by: taiL
16 Replies

8. UNIX for Dummies Questions & Answers

alias help

Hello again, what does \!* do int his line alias lookup 'grep -i \!* who.is.who' Thanks! (1 Reply)
Discussion started by: kris888
1 Replies

9. UNIX for Advanced & Expert Users

using alias...

hi.. i want to make an alias in unix, for using it to change of directory by example: if i am in /dtmp/inp/aux and i want to go to /sybase/bd, i want to make an alias named "bd", to go directally to /sybase/bd (alias bd="cd /sybase/bd") i create it, but when i turn off the conection... (2 Replies)
Discussion started by: DebianJ
2 Replies
Login or Register to Ask a Question

Featured Tech Videos