Reading quoted/escaped arguments correctly from a string

前端 未结 4 1656
难免孤独
难免孤独 2020-11-22 03:48

I\'m encountering an issue passing an argument to a command in a Bash script.

poc.sh:

#!/bin/bash

ARGS=\'\"hi there\" test\'
./swap ${ARGS}
<         


        
4条回答
  •  無奈伤痛
    2020-11-22 04:12

    This might not be the most robust approach, but it is simple, and seems to work for your case:

    ## demonstration matching the question
    $ ( ARGS='"hi there" test' ; ./swap ${ARGS} )
    there" "hi
    
    ## simple solution, using 'xargs'
    $ ( ARGS='"hi there" test' ; echo ${ARGS} |xargs ./swap )
    test hi there
    

提交回复
热议问题