map wget so that --no-check-certificate is automatically used

I want to map wget so that --no-check-certificate is used automatically. Here is my best attempt so far:

function wget() { wget @1 --no-check-certificate }

This isn't working great. For starters, I need to be able to accept an arbitrary number of arguments. I also am not sure if this is the correct way to "overwrite" wget, it seems too self-referential. What is a good way to make it so that wget automatically uses --no-check-certificate?

3 Answers

Just use an alias.

Aliases take precedence over built-ins and applications, so that you can overwrite applications.

Simply add this line to your .bashrc, .bash_aliases or similar:

alias wget='wget --no-check-certificate'

The alias will be expanded before it's executed. Anything after wget will be passed over to the command. Only then will it be executed like usual. This is possible because alias expansion is done by the shell, invoking the command, searching the path etc is done by the system (may be the kernel).

If you like to apply this behavior to any wget instance, even it is called by another application, use .wgetrc and add this line:

check-certificate = off

You have got the right basic idea, but there are some changes to be made:-

  1. Put the options first, including --no-check-certificate, before any URLs.
  2. To avoid any possible recursion, replace wget in the function body by $(which wget) when you declare the function definition. This works, because which is an external program, which therefore has no knowledge of alias or function definitions in bash.
  3. To pass the full function parameter string, replace your $1 by "$@". This expands into the full parameter string, with double-quotes around parameters to stop parameter splitting on embedded spaces. This works on Bourne-based shells (sh, ksh, bash, etc).

This all leads to the following function definition (using bash syntax):

wget() { $(which wget) --no-check-certificate "$@"; }

Using this, you can call wget with as many options and parameters as you want, and --no-check-certificate will always be included in the run string. You can add this to the shell's start-up file (eg ~/.bashrc).

If you want to use Wget, then why not use its configuration file? Don't mess with bash configuration files instead, that's klunky.

Wget has a configuration file, for one user typically:

$HOME/.wgetrc

A location for all users could be /usr/local/etc/wgetrc or /etc/wgetrc, depending on your install.

To disable ssl certificate validation for each invocation of wget (which you know is unsafe) you can use this directive in that file:

check_certificate = off

gnu manual for wget

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like