For example, if I have script ./foo that takes 10 parameters, and I only want to pass the 8th parameter. The only way I know how to do this currently is:
./foo '' '' '' '' '' '' '' 'bar'
Is there an easier/better way?
For example, if I have script ./foo that takes 10 parameters, and I only want to pass the 8th parameter. The only way I know how to do this currently is:
Is there an easier/better way? |
|||||
|
Another way to do this thing is to name the parameter you want to declare and then do so:
OUTPUT
In the above example the explicitly declared environment variable is preferred to the command-line argument, but the command-line argument is used when the environment variable is either empty or unset. When both the 8th positional and the environment variable The variable
... and the previous line omitted entirely but I wanted to demonstrate that declaring a variable in that way does result in a persistent value, and so I did it in two commands. Either way
For example:
OUTPUT
There we only see the eighth operand, but there are 101 of them globbed from my test directory. |
||||
|
Assuming you can change the the script, you should consider using optional parameters (options) instead of required parameters (arguments). If you have each of the first 7 seven parameters as options, and have them default to the empty string then you could just do:
If you use a POSIX-compatible shell you can use the Unless you implement something like the last X non-option arguments are the values for the last Y arguments and X-Y option arguments you have to provide option strings before each of the 7 (now empty) strings if you want to set any of those. This however is not common practise, normally an option is always an option and an argument an argument, and the order of option "insertion" is free. |
|||||
|