Quote:
Originally Posted by markseger
I'm not sure I follow, but I think understand functionally what you're suggesting and it sounds promising. what is exactly is xyzfunc? a one line script? I'm not sure what it would consist of. who calls the xyzfunc function?
|
It's really not that complicated, actually. It uses a
shell function, a block of code, sort of a mini-script, that resides in memory rather than a file. They are generally much more flexible and robust than a simple alias, since they can accept and process input parameters just like a script can.
The main problem you have here is that you need to alter the environment of the
parent shell
before the command line that executes the main script is parsed, and then again afterwards. The only way you can do this is with a command that is executed inside that shell itself; there's no way to do it from inside a script's sub-environment.
This means you must use aliases and/or functions. And the only way I can think of to create a one-word command that will do exactly what you want in the main shell is to use both.
Only an alias is capable of expanding a single command name into multiple commands before the rest of the line is parsed (meaning it's the only way you can turn off globbing). So we have to start with an alias. But as you know, it is then subsequently unable run another command after any arguments that follow it.
So what we do is use the alias to launch a function instead, and use that function to run the subsequent commands in the required order.
To break it down...
Code:
alias xyz='set -f ; xyzfunc'
With this alias, when you enter, for example, the line:
...it will replace the word "
xyz" at the front with the defined substitution string, so the line you are actually running is this:
Code:
set -f ; xyzfunc *.txt *.jpg
Since this expands to two separate commands, globbing can be disabled before the second command is run, and the arguments remain literal. So the alias executes the
xyzfunc function, with the two literal globbing values as arguments.
Now we've also previously defined the function
xyzfunc like this:
Code:
xyzfunc() { command xyz "$@" ; set +f ; }
(just put it in your shell startup file along with the alias.)
"
xyz" here is now the name of the actual script you want to run. The
command keyword is added to ensure that it doesn't do any recursive processing of the alias name, and just runs the script directly. "
$@" expands to a string of all the parameters passed to the function; here they become arguments to
xyz. Since and "
$@" is quoted (not to mention globbing is still off), they remain literal the whole time.
So the function launches two commands in sequence:
Code:
xyz *.txt *.jpg
set +f
...and so globbing is turned back on before it exits.