As an example of my problem:
#!/bin/bash
set -- "a b" "c d"
touch "$@"
ls -l "$@"
## I would like to save the positional parameters in a new variable x,
## then use $x exactly as per "$@".
## However, all these combinations fail.
x="$@"; ls -l $x
x="$@"; ls -l "$x"
x=$@; ls -l $x
x=$@; ls -l "$x"
The motivation for this is that I want to use the change of IFS +
redefinition of the positional parameters approach to process some
other variable (a la Bash FAQ E4). After this is done, I want to
restore the positional parameters to their original values.
Thanks in advance for any help.
$@ is a special variable when quoted. No other variable will behave
the same way from inside quotes. So what you must do is store them in
an array:
params=("$@") # To store the parameters
set -- "${params[@]}" # To restore the params
Hope that helps,
Moshe
--
*** SPAM BLOCK: Remove bra before replying! ***
Moshe Jacobson :: http://runslinux.net :: moshe at runslinux dot net
!! FBI Arreseting Programmers? http://www.freesklyarov.org
!! Trust Microsoft Anti-Trust: http://www.anti-dmca.org
In bash, use arrays:
saved_args=( "$@" )
and to restore:
set -- "${saved_args[@]}"
With Bourne or POSIX shells:
saved_args=
for i
do
saved_args=$saved_args\ \'`sed -e "
s/'/'\\\\\\\\''/g;\\\$s/\\\$/'/" << EOF
$i
EOF
`
done
and to restore:
eval set x "$saved_args"
shift
--
Stéphane