-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_eval.sh
executable file
·30 lines (26 loc) · 925 Bytes
/
test_eval.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/bin/bash
# This test demonstrates that using 'eval' and a variable containing
# a command that itself contains '"$@"' keeps the correct word splitting
# even when '@' has some array elements that contain spaces.
#
# This is because the command that gets eval'd contains double quotes
# around the '$@'
#
function print_args(){
for a in "$@"; do
echo "arg = '$a'"
done
}
function test_eval(){
cmd='print_args "$@"'
printf "echo \$cmd : \033[1;32m%s\033[0m\n" "$(echo $cmd)"
printf "eval echo \$cmd : \033[1;32m%s\033[0m\n" "$(eval echo $cmd)"
printf "eval \$cmd : ...\n"
eval $cmd
cmd_no_quotes='print_args $@'
printf "echo \$cmd_no_quotes : \033[1;32m%s\033[0m\n" "$(echo $cmd_no_quotes)"
printf "eval echo \$cmd_no_quotes : \033[1;32m%s\033[0m\n" "$(eval echo $cmd_no_quotes)"
printf "eval \$cmd_no_quotes : ...\n"
eval $cmd_no_quotes
}
test_eval A B C "D E F"