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
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
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:
The same applies to a loop, but the "trap" is a bit different:
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.
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)
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)
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)
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)
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)
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)
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)
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)