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

前端 未结 5 1392
孤街浪徒
孤街浪徒 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:14

    When you call your script pass the arguments within quotes.

    Example script:

    #!/bin/bash
    for arg in "$@"; do
     echo "arg: $1";
     shift;
    done
    

    When you call it with:

    ./program "parameter with multiple words" parameter2 parameter3 "another parameter"
    

    The output should be:

    arg: parameter with multiple words
    arg: parameter2
    arg: parameter3
    arg: another parameter
    

提交回复
热议问题