Bash citation problem

A="echo 'q'"

$A 

I got the result "q"

but if I find echo 'q' directly, it is q (without a single quote)

So, I wonder what rule bash follows when looking at single quotes inside double quotes.

Original problem

A="curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send"

$A

I got a curl: (6) Failed to resolve host 'poj.org' '

everything will be alright if I just type the command into the terminal.

PS I would like to use $ A to exclude a command inside A

+3
source share
3 answers

If you want to “save” a command for later execution, you do NOT want a variable. You need a function.

a() { curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send; }

, , . , , $A bash A, , , A , Pathname , , , . , ( [] "" ):

A:                        [echo 'q']
after wordsplitting:      [echo] ['q']
after pathname expansion: [echo] ['q']

bash echo 'q'.

bash, echo 'q' bash , bash . , . , bash ( ).

Recap: bash. ( , - , , , ). function.

+4

. BashFAQ/050: , !

, , .

.

, ?

, :

A=(curl http://123.196.125.62/send -H 'Host: poj.org' -e http://poj.org/send)
${A[@]}

, . man bash:

        , $, `,\,         !! $ `         .         , :        $, `,",\ <newline>.        , . ,         , ! .         . , ! .

+6

Better use backticks in this case:

A=`curl http://123.196.125.62/send -H 'Host: poj.org' -e 'http://poj.org/send'`
+2
source

Source: https://habr.com/ru/post/1789455/


All Articles