Y
Y
youngmysteriouslight2020-04-19 22:38:16
bash
youngmysteriouslight, 2020-04-19 22:38:16

How does bash expansion work?

Simplified version of the question.
Let there be two commands, one generates a set of parameters for the second. How to compose them?
I do:

$ cat args.sh 
Q="quoted arg"
U="unquoted arg"
echo -n "first \"second $Q\" $U"

$ cat multiarg.sh 
echo "1st: $1"
echo "2nd: $2"
echo "3rd: $3"

$ ./multiarg.sh $(./args.sh)
1st: first
2nd: "second
3rd: quoted

I want to get:
1st: first
2nd: second quoted arg
3rd: unquoted


How to achieve the desired behavior?
Why does the extension work like this in this example?

Original version of the question
Создал файл
$ cat ~/.local/bin/dcrm 
#/bin/bash
echo "-it -v \"$PWD\":/pr -w /pr -u $(id -u):$(id -g)"

$ dcrm
-it -v "/disk/Projects/graph":/pr -w /pr -u 1000:1000

Применяю:
$ docker run --rm $(dcrm) hello-world
docker: Error response from daemon: create "/disk/Projects/graph": "\"/disk/Projects/graph\"" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.

Ожидается, что это команда эквивалентна
docker run --rm -it -v "/disk/Projects/graph":/pr -w /pr -u 1000:1000 hello-world

Как видно, -it, -v и параметр -v передаются команде docker как отдельные параметры — всё как и задумано.
Однако, кавычки входят в состав параметра, что не есть хорошо. Убрать кавычки не могу, чтобы предотвратить расширение пути ($PWD) в случае наличия пробелов в нём.

Почему bash себя так ведёт?
Как правильно написать?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question