BASH alias not working...
Colin Watson
cjwatson at ubuntu.com
Tue Jun 21 00:27:18 UTC 2005
On Mon, Jun 20, 2005 at 08:14:41PM -0400, Matthew S-H wrote:
> On Jun 20, 2005, at 7:57 PM, Colin Watson wrote:
> >On Mon, Jun 20, 2005 at 07:43:24PM -0400, Matthew S-H wrote:
> >>This is the code I added to my "~/.bashrc":
> >>---START---
> >>alias "choose_to_edit"="cat /dev/null &&
> >> cd ~/Documents/ &&
> >> command ls | grep -n -v 'randomcraptonotmatch' | column &&
> >> echo 'Choose which one to edit:' &&
> >> read &&
> >> if test $REPLY -eq 1 ;
> >> then cat /dev/null &&
> >> echo 'test';
> >> fi"
> >>---END---
> >>Before I continued any further with this script, I wanted to test to
> >>see if that worked. However, it didn't...
> >>It gives we the following error:
> >>-bash: test: -eq: unary operator expected
> >You should quote "$REPLY" thus so that it expands to an empty word
> >rather than disappearing altogether if it's empty. You should probably
> >also use = rather than -eq so that test does string matching and
> >therefore doesn't complain about non-numeric arguments.
>
> This didn't do it. This compared the string "$REPLY" to 1.
"$REPLY" expands safely to the contents of the variable REPLY. $REPLY
expands unsafely to the contents of the variable REPLY.
Perhaps you're getting confused due to the way you've enclosed your
whole alias in "".
> When it executes "read", it gets a line of input from the user and
> places it in the environment variable "$REPLY".
> The "if test $REPLY -eq 1 ;" should compare this variable to the
> number 1.
As does 'if test "$REPLY" -eq 1;'.
> A revised version of my code:
> ---START---
> alias "choose_to_edit"="cat /dev/null &&
> cd ~/Documents/ &&
> command ls | grep -n -v 'randomcraptonotmatch' | column &&
> echo 'Choose which one to edit:' &&
> read &&
> if test '$REPLY' = 1 ;
> then cat /dev/null &&
> echo 'test';
> fi"
> ---END---
Now, *that* code will do the wrong thing that you thought my code would
do, if not enclosed within a huge "".
However, the way you've enclosed the whole alias in "" means that $REPLY
will get expanded at the time the alias is created. That will make your
code do very unexpected things. Unless you are extremely confident and
competent with shell quoting, you'd probably do well to take the advice
to use a script instead of an alias.
Cheers,
--
Colin Watson [cjwatson at ubuntu.com]
More information about the ubuntu-users
mailing list