title: Linux Bash: cURL - how to pass variables to the URL
body: <p>I want to do cURL GET-request. The following URL should be used:</p>
  <pre><code>https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi' -H 'Host: iant.toulouse.inra.fr' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' --compressed -H 'Referer: https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&amp;__wb_cookie_name=auth.rhime&amp;__wb_cookie_path=/bacteria/annotation/cgi&amp;__wb_session=WB84Qfsf&amp;__wb_main_menu=Genome&amp;__wb_function=$parent' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data '__wb_function=PortalExtractSeq&amp;mode=run&amp;species=rhime&amp;fastafile=%2Fwww%2Fbacteria%2Fannotation%2F%2Fsite%2Fprj%2Frhime%2F%2Fdb%2F$ab.genomic&amp;begin=$start&amp;end=$end&amp;strand=$strand
  </code></pre>
  <p>At the end of the URL, I have some words, which I want to design as variables, so depending on the input, the URL is different and I then request another resource.</p>
  <p>The end of the URL. $ab, $start, $end and $strand are the variables, all of them are Strings.</p>
  <pre><code>...2Frhime%2F%2Fdb%2F$ab.genomic&amp;begin=$start&amp;end=$end&amp;strand=$strand
  </code></pre>
  <p>I came across &quot;urlencode&quot; and I though of storing my URL as one big String in a variable and pass it to URL encode, but I am not sure, how to do it.</p>
  <p>I tried this/I am searching for something like this:</p>
  <pre><code>#!bin/bash
  [...]
  cURL=&quot;https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi' -H 'Host: iant.toulouse.inra.fr' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' --compressed -H 'Referer: https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&amp;__wb_cookie_name=auth.rhime&amp;__wb_cookie_path=/bacteria/annotation/cgi&amp;__wb_session=WB84Qfsf&amp;__wb_main_menu=Genome&amp;__wb_function=$parent' -H 'Content-Type: application/x-www-form-urlencoded' -H 'Connection: keep-alive' -H 'Upgrade-Insecure-Requests: 1' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data '__wb_function=PortalExtractSeq&amp;mode=run&amp;species=rhime&amp;fastafile=%2Fwww%2Fbacteria%2Fannotation%2F%2Fsite%2Fprj%2Frhime%2F%2Fdb%2F$ab.genomic&amp;begin=$start&amp;end=$end&amp;strand=$strand&quot;
  
  # storing HTTP response code in variable response. Only if the
  # reponse code is OK (200), we move on
    response=$(curl -X HEAD -I --header 'Accept:txt/html' &quot;https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&amp;__wb_cookie_name=auth.rhime&amp;__wb_cookie_path=/bacteria/annotation/cgi&amp;__wb_session=WB8jqwTM&amp;__wb_main_menu=Genome&amp;__wb_function=&quot;$location&quot;&quot;|head -n1|awk '{print $2}')
  
    echo &quot;$response&quot;
  
  # getting information via curl request
    if [ $response = 200 ] ; then
      info=$(curl -G &quot;$ (urlencode &quot;$cURL&quot;)&quot;)
    fi
  
    echo $info
  </code></pre>
  <p>For my response-code checkup, the method of directly passing $location seems to work, but with more variables, I get an error (response code 100, whereas I get 200 with the code-checkup)</p>
  <p>Do I have a general error in understanding curl/urlencode? What did I miss?</p>
  <p>Thanks for you time and effort in advance :)</p>
  <h1>UPDATE</h1>
  <pre><code>#!/bin/sh
  # handling command-line input
  file=$1
  ecf=$2
  
  
  # iterating through file and pulling out
  # information for the GET- and POST-request
  
  while read -r line
    do
      parent=$(echo $line | awk '{print substr($1,2,3)}')
      start=$(echo $line | awk '{print substr($2,2,6)}')
      end=$(echo $line | awk '{print substr($3,2,6)}')
      strand=$(echo $line | awk '{print substr($4,2,1)}')
      locus=$(echo $line | awk '{print substr($6,2,8)}')
  
  # depending on $parent, the right insertion for the URL is generated
      if [ $parent = &quot;SMc&quot; ] ; then
        location=&quot;Genome&quot;
        ab=&quot;SMc&quot;
      elif [ $parent = &quot;SMa&quot; ] ; then
        location=&quot;PrintPsyma&quot;
        ab=&quot;pSymA&quot;
      else [ $parent = &quot;SMb&quot; ]
        location=&quot;PrintPsymb&quot;
        ab=&quot;pSymB&quot;
      fi
  # building variables for curl content request
  
  
    options=( --compressed)
  
    headers=(
      -H 'Host: iant.toulouse.inra.fr'
      -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:56.0) Gecko/20100101 Firefox/56.0'
      -H 'Accept: txt/html,application/xhtml+xml,application/xml;1=0.9,*/*;q=0.8'
      -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3'
      -H 'Referer: https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&amp;__wb_cookie_name=auth.rhime&amp;__wb_cookie_path=/bacteria/annotation/cgi&amp;__wb_session=WB84Qfsf&amp;__wb_main_menu=Genome&amp;__wb_function=$parent'
      -H 'Content-Type: application/x-www-form-urlencoded'
      -H 'Connection: keep-alive'
      -H 'Upgrade-Insecure-Requests: 1'
      -H 'Pragma: no-cache'
      -H 'Cache-Control: no-cache'
    )
  
      url='https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi'
  
      ab=$(urlencode &quot;${ab}&quot;)
      start=$(urlencode &quot;${start}&quot;)
      end=$(urlencode &quot;${end}&quot;)
      strand=$(urlencode &quot;${strand}&quot;)
      data=&quot;__wb_function=PortalExtractSeq&amp;mode=run&amp;species=rhime&amp;fastafile=%2Fwww%2Fbacteria%2Fannotation%2F%2Fsite%2Fprj%2Frhime%2F%2Fdb%2F$ab.genomic&amp;begin=$start&amp;end=$end&amp;strand=$strand&quot;
  
  
  
  
  # storing HTTP response code in variable response. Only if the
  # reponse code is OK (200), we move on
      response=$(curl -X HEAD -I --header 'Accept:txt/html' &quot;https://iant.toulouse.inra.fr/bacteria/annotation/cgi/rhime.cgi?__wb_cookie=&amp;__wb_cookie_name=auth.rhime&amp;__wb_cookie_path=/bacteria/annotation/cgi&amp;__wb_session=WB8jqwTM&amp;__wb_main_menu=Genome&amp;__wb_function=&quot;$location&quot;&quot;|head -n1|awk '{print $2}')
  
      echo &quot;$response&quot;
  
  # getting information via curl request
      if [ $response = 200 ] ; then
          info=$(curl -G &quot;${options[@]}&quot; &quot;${headers[@]}&quot; --data &quot;${data}&quot; &quot;${url}&quot;)
      fi
  
      echo $info
  
  done &lt; $file
  </code></pre>