Mailinglist Archive: opensuse (2496 mails)

< Previous Next >
Re: [opensuse] .bashrc alias - One Liner Problem?

----- Original Message -----
From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
To: "suse" <opensuse@xxxxxxxxxxxx>
Sent: Monday, December 01, 2008 2:23 PM
Subject: Re: [opensuse] .bashrc alias - One Liner Problem?


Anders Johansson wrote:
On Monday 01 December 2008 19:12:24 David C. Rankin wrote:
I finally resolved to the function idea Aaron first suggested.

Who is Aaron?


Kulkis


This is pointless. "wp konqueror" expands to

wpsearch $1 konqueror

and the only reason your alias works is that this is a valid string, since
$1
is the empty string after expansion. But try starting this from a script
where
you pass parameters. Then $1 won't be empty, and then you will get weird
results

I guess I've been lucky using $1 with aliases. I don't know how they work,
but they work in most instances for me:

alias hist='history | grep $1' #Requires one input

Same thing for all of these. "hist konqueror" expands to

history | grep $1 konqueror

which works because $1 is the empty string. tmsg -f expands to

sudo tail $1 -n 50 /var/log/messages -f

which again is a valid command line

And so on and so on and so forth

Anders

Thanks Anders,

Lucky was the operative word. I will find whatever howto suggested this in the
first place and convey the information. I think I still have the link
somewhere. Further experimentation shows, as always, you are dead-on:

alias tstinput='echo -e "\ntest\n\$2=$2; \n\$1=$1; \n\$3=$3\n"'
. .bashrc
13:19 alchemy~> tstinput one two three

test
$2=;
$1=;
$3=
one two three

Mystery solved.

The how-to may not necessarily be wrong. einstein was right, everything is
relative, and what is untrue in one context may be true in some other.

If you had a script, you could put som aliases in the script and, depending on
your quoting/escaping syntax, you could put $1,$2 etc in the alias.
What would happen is a one-time fixed expansion would happen where the args
given on the script command line would get expaded and inserted into the alias.

So if you have a script named myscript like this:
---TOF---
#!/bin/sh
alias show1="echo arg1 is $1"
alias show2="echo arg2 is $2"

show1
show2
---EOF---

Notice the alias is not single-quoted, and so the $1 gets expanded immediately
at the time the alias definition is read and assigned.

And the you ran the script like this:
myscript hello there

the output would be
arg1 is hello
arg2 is there

if the script had these lines instead:
show1 foo bar
show2 foo bar

the output would be:
arg1 is hello foo bar
arg2 is there foo bar

The foo bar are not being passe as arguments to the alias.
Rather the alias is expanding in-place right there on that line as if it were a
variable.
so
at the time the alias is defined,
alias show1="echo arg1 is $1"
becomes
alias show1="echo arg1 is hello"

then later when you use it,
show1 foo bar
becomes
echo arg1 is hello foo bar
and then this echo command is run.


Now, If you single-quote the alias, you get a slightly different result.
Then the $1 is not expanded at the time the alias is defined, but it will be
expanded later at run-time, but it will end up expanding to the same value,
which is $1 to the script, not what looks like arguments passed to the alias.
The end result is the same (so far) but notice the different order that things
happen:

at the time the alias is defined
alias show1='echo arg1 is $1'
the $1 stays a litteral $1

so then at run-time:
show1 foo bar
becomes
echo arg1 is $1 foo bar
which then becomes
echo arg1 is hello foo bar

Now the part that might cause confusion if you don't understand the various
orders of operation and nesting and quoting etc...

If you have the alias, which includes $1, inside a script, and it's
single-quoted,
Then it starts out producing the same result as the first example, just
expanding to the scripts original command line arguments. But, since the $1 is
expanding at the last minute, then it will reflect changes to the current
scripts argv as a result of the shift and set commands.

so take this script:

---TOF---
#!/bin/sh
alias show1='echo arg1 is $1'
alias show2='echo arg2 is $2'

show1
show2

echo using shift once
shift

show1
show2

echo using set to set a new argv
set holy cow

show1
show2

echo adding foo bar at run-time
show1 foo bar
show2 foo bar
---EOF---


You get the following output:

whoa:~ $ ./myscript hello there
arg1 is hello
arg2 is there
using shift once
arg1 is there
arg2 is
using set to set a new argv
arg1 is holy
arg2 is cow
adding foo bar at run-time
arg1 is holy foo bar
arg2 is cow foo bar
whoa:~ $


Normally in .basrc there is no argv because no command line args are passed to
the initial interactive shell. But, if .bashrc ever gets invoked at any other
times, maybe then sometimes there are command line arguments. So it's POSSIBLE
that there is some use for aliases that include $1,$2,etc in .bashrc , but I
don't know any off hand.

So depending on what exactly the how-to was about, and what exact context it
was talking about, it wasn't automatically wrong just because it involved an
alias that uncludes a $1

--
Brian K. White brian@xxxxxxxxx http://www.myspace.com/KEYofR
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx Linux SCO FreeBSD #callahans Satriani Filk!

--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >