0

I am writing a script to upload a file on server by using Curl.


while [ "$1" != "" ] ; do

    case "$2" in
    -upload)
        curl -X POST -F 'file=@$2' -F '....' http://....
        printf "\n"
        ;;  
    *)
        echo 'unknown argument'
        exit -1
        ;;
  esac
  shift
done 

The thing is I don't know how to write $1 in the curl properly ('file=@$1') . When I use this script, it always announce error: curl: couldn't open file "$1"

chipbk10
  • 5,783
  • 12
  • 49
  • 85
  • 1
    possible duplicate of [Expansion of variable inside single quotes in a command in bash shell script](http://stackoverflow.com/questions/13799789/expansion-of-variable-inside-single-quotes-in-a-command-in-bash-shell-script) – Etan Reisner Oct 14 '14 at 13:05

1 Answers1

2

Try to use " (double quotes) instead of '

curl -X POST -F "file=@$1" -F '....' http://....

Single quotes make the $1 to be not expanded to the argument.

Fazlin
  • 2,285
  • 17
  • 29
  • so do you know, why I always get "unknown argument' printed on the screen? – chipbk10 Oct 14 '14 at 13:06
  • `error: curl: couldn't open file "$1"` occurs because you have single quotes which makes bash to consider the `$1` as string rather than a variable. Putting it inside double-quotes makes bash expand `$1` to the first argument. Try the difference between `echo "$1"` and `echo '$1'` – Fazlin Oct 14 '14 at 13:09