Bash: pass variable as a single parameter / shell quote parameter

前端 未结 5 1431
孤街浪徒
孤街浪徒 2021-01-23 02:32

I\'m writing a bash script which has to pass a variable to another program:

./program $variable

The problem is, it is absolutely necessary for

5条回答
  •  梦谈多话
    2021-01-23 03:16

    The problem seems to be inside the "program"

    variable="Hello World"    # quotes are needed because of the space
    ./program "$variable"     # here quotes again
    

    and inside the program

    echo "program received $# arguments:" 
    i=1
    for arg in "$@"      # again quotes needed
    do echo "arg $((i=i+1)): '$arg'"    # and again
    done
    

提交回复
热议问题