BASH alias not working...

Hal Finkel Hal at Finkel.com
Tue Jun 21 00:39:29 UTC 2005


The problem is that you need to prevent the variables (like $REPLY) from
being expanded when the alias is defined. So try something like this:

---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---

 -Hal

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.
> 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.
>
> 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---
>
>
> Thanks again,
> ~Matt
>





More information about the ubuntu-users mailing list