How can I store $@
in a variable while keeping its properties?
I want to obtain exactly the same behavior even if I use $@
or my own variable in all possible situations.
The attempts below didn't work:
args=$@
args2="$@" # the arguments are combined (see the last output paragraph)
I tested them using the following line:
s.sh A B=" " C
s.sh
#!/bin/bash
args=$@
args2="$@"
#args3 = ? - TODO
echo $args
echo $args2
echo $@
#echo $args3
echo
echo "$args"
echo "$args2"
echo "$@"
#echo "$args3"
echo
java A $args
java A $args2
java A $@
#java A $args3
echo
java A "$args"
java A "$args2"
java A "$@"
#java A "$args3"
A.java
import java.util.Arrays;
public class A {
public static void main(String args[]){
System.out.println(args.length + ": " + Arrays.asList(args));
}
}
Actual output:
A B= C
A B= C
A B= C
A B= C
A B= C
A B= C
3: [A, B=, C]
3: [A, B=, C]
3: [A, B=, C]
1: [A B= C]
1: [A B= C]
3: [A, B= , C]
Expected output (if TODO is replaced by a solution) and echo
s are enabled
A B= C
A B= C
A B= C
A B= C
A B= C
A B= C
A B= C
A B= C
3: [A, B=, C]
3: [A, B=, C]
3: [A, B=, C]
3: [A, B=, C]
1: [A B= C]
1: [A B= C]
3: [A, B= , C]
3: [A, B= , C]