Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script to receive and repass quoted parameters

Tags:

bash

unix

I'm trying to get quoted parameters of a bash script to safely be received by a nested script. Any ideas?

test.sh

#!/bin/bash echo $* bash myecho.sh $* 

myecho.sh

#!/bin/bash  echo $1  echo $2  echo $3  echo $4 

Sample:

bash test.sh aaa bbb '"ccc ddd"' 

Result:

aaa bbb "ccc ddd" aaa bbb "ccc ddd" 

Wanted result

aaa bbb "ccc ddd" aaa bbb ccc ddd 
like image 700
chilltemp Avatar asked Jan 15 '09 20:01

chilltemp


People also ask

How do you pass quotes in a bash script?

When you want to pass all the arguments to another script, or function, use "$@" (without escaping your quotes). See this answer: Difference between single and double quotes in Bash.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

How do you pass parameters in Shellscript?

Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


2 Answers

You want to use "$@" (quoted dollar at) to pass parameters to a subscript. Like so ....

ls-color.sh:

#!/bin/bash /bin/ls --color=auto "$@"    # passes though all CLI-args to 'ls' 


As to why.....

From the Bash man page:

$* -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, "$*" is equivalent to "$1c$2c...", where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.

$@ -- Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, "$@" is equivalent to "$1" "$2" ... If the double-quoted expansion occurs within a word, the expansion of the first parameter is joined with the beginning part of the original word, and the expansion of the last parameter is joined with the last part of the original word. When there are no positional parameters, "$@" and $@ expand to nothing (i.e., they are removed).


Setting up some demo scripts ...

echo 'echo -e "\$1=$1\n\$2=$2\n\$3=$3\n\$4=$4"' > echo-params.sh echo './echo-params.sh $*' > dollar-star.sh echo './echo-params.sh $@' > dollar-at.sh echo './echo-params.sh "$*"' > quoted-dollar-star.sh echo './echo-params.sh "$@"' > quoted-dollar-at.sh chmod +x *.sh 

"$@" - quoted-dollar-at is an identity transformation for re-passing args to a subshell (~99% of the time, this is what you meant to do):

./quoted-dollar-at.sh aaa '' "'cc cc'" '"ddd ddd"'   # $1= aaa   # $2=               # $3= 'cc cc'   # $4= "ddd ddd" 

"$*" - quoted-dollar-star smashes the args into a single string (~1% of the time you actually want this behavior, eg in a conditional: if [[ -z "$*" ]]; then ...):

./quoted-dollar-star.sh aaa '' "'cc cc'" '"ddd ddd"'   # $1= aaa  'cc cc' "ddd ddd"      # $2=                        # $3=                # $4= 

$* / $@ - without quotes, both forms strip off one level of quotation and interpret spaces from the underlying strings but ignore quotation characters (almost always, this is a mistake):

./dollar-star.sh aaa '' "'cc cc'" '"ddd ddd"'   # $1= aaa   # $2= 'cc                     # $3= cc'   # $4= "ddd  ./dollar-at.sh aaa '' "'cc cc'" '"ddd ddd"'   # $1= aaa   # $2= 'cc   # $3= cc'   # $4= "ddd 

If you want to have some fun, you can use "$@" to nest things as deep as you'd like, pushing and popping elements off the args stack if you like.

function identity() {   "$@" } set -x identity identity identity identity identity echo Hello \"World\" # + identity identity identity identity identity echo Hello '"World"' # + identity identity identity identity echo Hello '"World"' # + identity identity identity echo Hello '"World"' # + identity identity echo Hello '"World"' # + identity echo Hello '"World"' # + echo Hello '"World"' # Hello "World" 
like image 129
Dave Dopson Avatar answered Nov 15 '22 16:11

Dave Dopson


#!/bin/bash echo $* bash myecho.sh "$@" 

Note the "$@" construct is not bash specific and should work with any POSIX shell (it does with dash at least). Note also that given the output you want, you don't need the extra level of quoting at all. I.E. just call the above script like:

./test.sh 1 2 "3 4" 
like image 31
pixelbeat Avatar answered Nov 15 '22 18:11

pixelbeat