[olug] bash script help

William E. Kempf williamkempf at hotmail.com
Thu Oct 3 18:58:46 UTC 2002


I've got a bit of a complex bash script problem.  I need to change the value
of the last positional argument passed to the script, in order to pass the
arguments on to another script/program.  Here's my first (naive) attempt:

# script "test"
let idx=$#-1
set "${@:0:$idx} new-value"
echo "$@"

The problem with this is in quoting (which often makes my head hurt).
Here's a sample usage that works:

$test foo bar widget
foo bar new-value

However, this usage doesn't work correctly:

$test "foo bar" widget
foo bar new-value

What I need would be this output instead:

"foo bar" new-value

Anyone know of a way to achieve this?  I suppose I could take a brute force
approach and loop through every argument appending them to a variable with
quotes around it... but the extra quotes this would produce bug me a bit,
and this seems like it would be an awfully slow way to do this.

Bill Kempf



More information about the OLUG mailing list