0

I've looked over a lot of docs, discussions and tutorials but don't see what's causing a string to not be expanded into parameters using a BASH script. The argument is being passed from an Ubuntu 18.04 host to a container running Ubuntu 14.04.

Here's the whole string output from echo $@

'/mnt/pathfinder/contexts/chase/software/util/scripts/perception/movingObstacleDetection/training/cloud/buildTrainTestForMOD2.py --traindir /mnt/pathfinder/logs/other/MOD2/classifiers/2022.01.06_23.19.24__chase --context chase' /mnt/pathfinder/logs/other/MOD2/classifiers/2022.01.06_23.19.24__chase/build_dataset_training_output.txt

I want to assign the portion within single quotes to $1 however arg1=$1 outputs

'/mnt/pathfinder/contexts/chase/software/util/scripts/perception/movingObstacleDetection/training/cloud/buildTrainTestForMOD2.py

So it appears to be splitting on whitespace instead. In a terminal shell (the default version with Ubuntu 18.04) I get the expected parameter expansion with $1.

What am I missing?

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • `echo $@` is _exactly_ the same with `echo $*` -- with both of them you lose your syntactic quotes and see only literal ones. – Charles Duffy Jan 07 '22 at 01:15
  • 1
    What I mean by that is that `echo $@` output will look _exactly_ identical between `./yourscript "first argument" "second argument"`, and `./yourscript "first" "argument" "second" "argument"` and `./yourscript first argument second argument`; the information about where the original argument boundaries were is thrown away... – Charles Duffy Jan 07 '22 at 01:16
  • 2
    ...so if you have single quotes showing up in the output of `echo`, that means those quotes are _part of your data_ (being treated as filename elements), not _part of your syntax_ as they should be (indicating where each "word" of data begins and ends). – Charles Duffy Jan 07 '22 at 01:17
  • 1
    If you want to get a better view of what your argument list is, use `printf '%q\n' "$@"` -- with `./yourscript "first world" "second word"`, for example, that will give you one line with something like `first\ word` and a second one with something like `second\ word` ("something like" because the exact quoting/escaping is an implementation detail and can vary between versions). – Charles Duffy Jan 07 '22 at 01:18
  • 2
    It'll depend on exactly how the parameter(s) are being passed (from whatever's on the host, via some path into the container, to whatever's using them there), but these may help explain what's going on: ["Why does shell ignore quoting characters in arguments passed to it through variables?"](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia) and [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050) – Gordon Davisson Jan 07 '22 at 01:36

0 Answers0