There are legitimate technical reasons to want a generalized solution to the problem of bash alias not having a mechanism to take a reposition arbitrary arguments. One reason is if the command you wish to execute would be adversely affected by the changes to the environment that result from executing a function. In all other cases, functions should be used.

What recently compelled me to attempt a solution to this is that I wanted to create some abbreviated commands for printing the definitions of variables and functions. So I wrote some functions for that purpose. However, there are certain variables which are (or may be) changed by a function call itself. Among them are:

FUNCNAME BASH_SOURCE BASH_LINENO BASH_ARGC BASH_ARGV

The basic command I had been using (in a function) to print variable defns. in the form output by the set command was:

sv () { set | grep --color=never -- "^$1=.*"; }

E.g.:

> V=voodoo sv V V=voodoo

Problem: This won't print the definitions of the variables mentioned above as they are in the current context, e.g., if in an interactive shell prompt (or not in any function calls), FUNCNAME isn't defined. But my function tells me the wrong information:

> sv FUNCNAME FUNCNAME=([0]="sv")

One solution I came up with has been mentioned by others in other posts on this topic. For this specific command to print variable defns., and which requires only one argument, I did this:

alias asv='(grep -- "^$(cat -)=.*" <(set)) <<<'

Which gives the correct output (none), and result status (false):

> asv FUNCNAME > echo $? 1

However, I still felt compelled to find a solution that works for arbitrary numbers of arguments.

A General Solution To Passing Arbitrary Arguments To A Bash Aliased Command:

# (I put this code in a file "alias-arg.sh"): # cmd [arg1 ...] – an experimental command that optionally takes args, # which are printed as "cmd(arg1 ...)" # # Also sets global variable "CMD_DONE" to "true". # cmd () { echo "cmd($@)"; declare -g CMD_DONE=true; } # Now set up an alias "ac2" that passes to cmd two arguments placed # after the alias, but passes them to cmd with their order reversed: # # ac2 cmd_arg2 cmd_arg1 – calls "cmd" as: "cmd cmd_arg1 cmd_arg2" # alias ac2=' # Set up cmd to be execed after f() finishes: # trap '\''cmd "${CMD_ARGV[1]}" "${CMD_ARGV[0]}"'\'' SIGUSR1; # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ # (^This is the actually execed command^) # # f [arg0 arg1 ...] – acquires args and sets up trap to run cmd: f () { declare -ag CMD_ARGV=("$@"); # array to give args to cmd kill -SIGUSR1 $$; # this causes cmd to be run trap SIGUSR1; # unset the trap for SIGUSR1 unset CMD_ARGV; # clean up env... unset f; # incl. this function! }; f' # Finally, exec f, which will receive the args following "ac2".

E.g.:

> . alias-arg.sh > ac2 one two cmd(two one) > > # Check to see that command run via trap affects this environment: > asv CMD_DONE CMD_DONE=true

A nice thing about this solution is that all the special tricks used to handle positional parameters (arguments) to commands will work when composing the trapped command. The only difference is that array syntax must be used.

E.g.,

If you want "$@", use "${CMD_ARGV[@]}".

If you want "$#", use "${#CMD_ARGV[@]}".

Etc.