bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Expanding aliases to full command before execution


From: dethrophes
Subject: Re: Expanding aliases to full command before execution
Date: Wed, 04 Apr 2012 21:12:33 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:11.0) Gecko/20120327 Thunderbird/11.0.1

Am 04.04.2012 17:27, schrieb address@hidden:
On Tue, Apr 3, 2012 at 5:22 PM, address@hidden<address@hidden>  wrote:
Hi everyone,

In bash, is it possible to expand my aliases either before they are executed
or when they are stored in the history file?
For example, if I have:  alias ll='ls -l'  defined in my .bashrc, when I
execute "ll" from the command line, I'd like my history file to contain "ls
-l".  Is there any way to accomplish this?

Thanks,
Justin

I seem to have constructed a solution to my own problem.  I wrote a
function that creates the desired behavior for me and saves it in
$expanded if the argument was in fact an alias.

function expand_alias()                 # expand an alias to full command
{
     if [ "$1" = "." ]; then
         argument="\\$1"
     else
         argument="$1"
     fi
     match=$( alias -p | grep -w "alias $argument=" )
     if [ -n "$match" ]; then
         expanded="`echo $match | sed -e s/[^=]*=// | sed 's/^.\(.*\).$/\1/'`"
     else
         expanded="$1"
     fi
}


Thanks,
Justin

or you could just do

function expand_alias()                 # expand an alias to full command
{
    if expanded=$( alias "${1}" ); then
        expanded="${expanded#*=}"
    else
        expanded="$1"
    fi
}

though depending on what your actually trying to do you'd be getter of using type, as not all aliases are aliases a lot are functions these days.



    case $(type -t "$c") in
      "")
        echo No such command "$c"
        return 127
        ;;
      alias)
        c="$(type "$c"|sed "s/^.* to \`//;s/.$//")"
        ;;
      function)
        c=$(type "$c"|sed 1d)";\"$c\""
        ;;
      *)
        c="\"$c\""
        ;;
    esac

bash -xvc "$c"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]