I'm writing a very simple script that will call another script and need to propagate parameters from the current script to the executing script.
For example, my script name is foo.sh and calls bar.sh
foo.sh:
bar $1 $2 $3 $4
How do I do this without explicitly specifying each parameter?
#1 building
#!/usr/bin/env bash while [ "$1" != "" ]; do echo "Received: ${1}" && shift; done;
Just thought it might be more useful when trying to test how args gets into the script
#2 building
My SUN Unix has many limitations, even "$@" can't be explained on demand. My solution is ${@}. For example,
#!/bin/ksh find ./ -type f | xargs grep "${@}"
By the way, I have to have this particular script because my Unix doesn't support grep-r either
#3 building
For bash and other Bourne like shells:
java com.myserver.Program "$@"
#4 building
Works unless you have spaces or escape characters. In this case, I couldn't find a way to capture the parameters and send them to ssh within the script.
It may be useful, but it's ugly
_command_opts=$( echo "$@" | awk -F\- 'BEGIN { OFS=" -" } { for (i=2;i<=NF;i++) { gsub(/^[a-z] /,"&@",$i) ; gsub(/ $/,"",$i );gsub (/$/,"@",$i) }; print $0 }' | tr '@' \' )
#5 building
I know this question has been answered well, but it's a comparison between "$@" $@ "$*" and $*
Content of test script:
# cat ./test.sh #!/usr/bin/env bash echo "=================================" echo "Quoted DOLLAR-AT" for ARG in "$@"; do echo $ARG done echo "=================================" echo "NOT Quoted DOLLAR-AT" for ARG in $@; do echo $ARG done echo "=================================" echo "Quoted DOLLAR-STAR" for ARG in "$*"; do echo $ARG done echo "=================================" echo "NOT Quoted DOLLAR-STAR" for ARG in $*; do echo $ARG done echo "================================="
Now, run the test script with various parameters:
# ./test.sh "arg with space one" "arg2" arg3 ================================= Quoted DOLLAR-AT arg with space one arg2 arg3 ================================= NOT Quoted DOLLAR-AT arg with space one arg2 arg3 ================================= Quoted DOLLAR-STAR arg with space one arg2 arg3 ================================= NOT Quoted DOLLAR-STAR arg with space one arg2 arg3 =================================