curl command not executing via shell script in bash
Solution 1
A command embedded within a parenthesis runs as a sub-shell so your environment variables will be missing.
Try eval:
curlCmd="curl 'https://www.facebook.com/vivekkumar27june88' > ~/Desktop/fb.html"
eval $curlCmd
Solution 2
Create your script t.sh
as this single line only:
curl -k "https://www.facebook.com/vivekkumar27june88" -o ~/Desktop/fb.html
As per man curl
:
-k, --insecure
(SSL) This option explicitly allows curl to perform "insecure" SSL connections transfers.
All SSL connections are attempted to be made secure by using the CA certificate bundle
installed by default. This makes all connections considered "insecure" fail unless -k,
--insecure is used.
-o file
Store output in the given filename.
Solution 3
As @Chepner said, go read BashFAQ #50: I'm trying to put a command in a variable, but the complex cases always fail!. To summarize, how you should do things like this depends on what your goal is.
-
If you don't need to store the command, don't! Storing commands is difficult to get right, so if you don't need to, just skip that mess and execute it directly:
curl "https://www.facebook.com/vivekkumar27june88" > ~/Desktop/fb.html
-
If you want to hide the details of the command, or are going to use it a lot and don't want to write it out each time, use a function:
curlCmd() { curl "https://www.facebook.com/vivekkumar27june88" } curlCmd > ~/Desktop/fb.html
-
If you need to build the command piece-by-piece, use an array instead of a plain string variable:
curlCmd=(curl "https://www.facebook.com/vivekkumar27june88") for header in "${extraHeaders[@]}"; do curlCmd+=(-H "$header") # Add header options to the command done if [[ "$useSilentMode" = true ]]; then curlCmd+=(-s) fi "${curlCmd[@]}" > ~/Desktop/fb.html # This is the standard idiom to expand an array
-
If you want to print the command, the best way to do it is usually with
set -x
:set -x curl "https://www.facebook.com/vivekkumar27june88" > ~/Desktop/fb.html set +x
...but you can also do something similar with the array approach if you need to:
printf "%q " "${curlCmd[@]}" # Print the array, quoting as needed printf "\n" "${curlCmd[@]}" > ~/Desktop/fb.html
Vivek Kumar
Updated on July 09, 2022Comments
-
Vivek Kumar almost 2 years
I'm learning shell scripting! for the same I've tried downloading the facebook page using curl on ubuntu terminal.
t.sh content
vi@vi-Dell-7537(Desktop) $ cat t.sh curlCmd="curl \"https://www.facebook.com/vivekkumar27june88\"" echo $curlCmd ($curlCmd) > ~/Desktop/fb.html
Getting error when running the script as
vi@vi-Dell-7537(Desktop) $ ./t.sh curl "https://www.facebook.com/vivekkumar27june88" curl: (1) Protocol "https not supported or disabled in libcurl
But if the run the command directly then it is working fine.
vi@vi-Dell-7537(Desktop) $ curl "https://www.facebook.com/vivekkumar27june88" <!DOCTYPE html> <html lang="hi" id="facebook" class="no_js"> <head><meta chars.....
I will appreciate if anyone let me know the mistake I am doing in the script.
I've verified that curl library have ssl enabled.
-
chepner over 8 years
-
-
Vivek Kumar over 8 yearsHI anubhava! same error even after adding -k flag to curl.
-
anubhava over 8 yearsCheck path of curl in your shell by running
type curl
and the check same inside the script. -
Vivek Kumar over 8 yearswhen running
$type curl
getting the following outputcurl is hashed (/usr/bin/curl)
. But from script output iscurl is /usr/bin/curl
. Can you please tell me what hashed means here?? -
anubhava over 8 yearsHmm both seem to be running same
curl
binary. Not sure what could be the different from shell to your script. So copy/paste ofcurl -k "https://www.facebook.com/vivekkumar27june88" -o ~/Desktop/fb.html
works fine butbash ./t.sh
gives that error? -
Vivek Kumar over 8 yearsBut I don't understand why script is making this difference. Can you please explain...Thanks :)
-
anubhava over 8 yearsNot sure why it doesn't work for you as I have tried this
curl
command from shell as well from the script and it has worked fine both ways. -
Vivek Kumar over 8 yearsNo $cmd is not working in my case on ubuntu 14.04....
vi@vi-Dell-7537(Desktop) $ ./t.sh curl -k "https://www.facebook.com/vivekkumar27june88" curl: (1) Protocol "https not supported or disabled in libcurl vi@vi-Dell-7537(Desktop) $ cat t.sh #!/bin/sh curlCmd="curl -k \"https://www.facebook.com/vivekkumar27june88\"" echo $curlCmd $curlCmd > ~/Desktop/fb.html
-
Gordon Davisson over 8 yearsIn this case,
eval
is needed to get the single-quotes in the command string interpreted. Buteval
comes with a huge collection of traps for the unwary, and you're much better off avoiding it. There are much better ways to do this, like storing the command as an array (or not storing it at all!).