1

I want to insert into variable anything I send inside ".

For example:

check.sh:

#!/bin/bash
./b.sh -a "$@"

b.sh:

#!/bin/bash

while getopts ":a:b:c:" opt; do
  case ${opt} in
        a) A="$OPTARG"
;;
        b) B="$OPTARG"
;;
        c) C="$OPTARG"
;;
        :) echo "bla"
exit 1
;;
esac
done

echo "a: $A, b: $B, c: $C"

Run #1: Desired result:

user@host $  ./check.sh -a asd -b "asd|asd -x y" -c asd
a: -a asd -b "asd|asd -x y" -c asd, b: ,c: 

Actual result:

user@host $  ./check.sh -a asd -b "asd|asd -x y" -c asd
a: -a, b: , c:

Run #2: Desired result:

user@host $ ./check_params.sh -a asd -b asd|asd -c asd
a: -a asd -b asd|asd -c asd, b: ,c:

Actual result:

user@host $ ./check_params.sh -a asd -b asd|asd -c asd
-bash: asd: command not found
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Nir
  • 2,497
  • 9
  • 42
  • 71
  • 1
    check.sh is adding `-a` before the arg list it gets, so it's effectively running `./b.sh -a -a asd -b "asd|asd -x y" -c asd` -- and the doubled `-a` is causing lots of confusion. – Gordon Davisson Jun 20 '19 at 17:40

1 Answers1

0

Use $* instead of $@:

check.sh:

#!/bin/bash
./b.sh -a "$*"

"$*" is a string representation of all positional parameters joined together with $IFS variable. Whereas $@ expands into separate arguments.

Also note that in your 2nd example you need to use quote pipe character string:

./check.sh -a asd -b 'asd|asd' -c asd

Check: What is the difference between “$@” and “$*” in Bash?

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • What if I need `'` to be forwarded as well? – Nir Jun 20 '19 at 15:11
  • For example `./check.sh -a asd -b 'asd asd' -c asd` for that output should be: `a: -a asd -b 'asd asd' -c asd, b: ,c:` – Nir Jun 20 '19 at 15:18
  • You should just quote it: `./check.sh "-a asd -b 'asd asd' -c asd"` – anubhava Jun 20 '19 at 15:22
  • I can't. `check.sh` actually represent lots of different scripts that sometime call other common script `b.sh` – Nir Jun 20 '19 at 15:29
  • Your question is about `How to get all input inside quote into variable` so solution is also for that. In Unix shell quoting is very critical and not correctly quoting can change outcome drastically. – anubhava Jun 20 '19 at 15:31
  • That is correct, but the solution I need is for the `b.sh` script - not for calling `check.sh` as I can't handle input. Run #1 simulate that desire. – Nir Jun 20 '19 at 15:44
  • 1
    @Nir You might want to start by reading [Bash FAQ 050](https://mywiki.wooledge.org/BashFAQ/050). – chepner Jun 20 '19 at 15:53
  • I understand what you are saying. Unfortunately this is the case. I already started to look at options like `${*//asd asd/'asd asd'}` but it did not sort the issue as the destination script saw it as `'asd` – Nir Jun 20 '19 at 16:55