Because sometimes you just want to f-ing ping!
Modern browsers, believing they are being clever, hide the protocol in the URL bar.
However, even if you only select the hostname, when you paste that URL into your terminal, you will encounter the following:
This will inevitably fail due to the browser's "helpful" addition of the protocol and a trailing slash to the hostname you've copied.
This basic Bash function alters the ping command, allowing it to accept URLs as well.
Example:
ping https://google.com
instead of having to remove everything but the hostname i.e.
ping google.com
The entirety of the function is:
function ping() {
local new_args=()
local url_found=0
for arg in "$@"; do
if [[ "$arg" =~ ^http ]] && [ $url_found -eq 0 ]; then
# Process the URL
local url="$arg"
url=${url#*://} # Remove protocol
url=${url%%/*} # Remove path
url=${url%%:*} # Remove port
url=${url%%@*} # Remove user info
new_args+=("$url")
url_found=1
else
# Add the argument as is
new_args+=("$arg")
fi
done
# Call the original ping command with the new argument list
command ping "${new_args[@]}"
}
To add the ping
function to your shell environment so that it's available every time you open a terminal, you'll need to add it to your shell's configuration file. For Bash, this is typically .bashrc
, and for Zsh, it's .zshrc
. Here are the steps for both:
-
Open your
.bashrc
file in a text editor. You can use a command-line editor likenano
orvi
, or any graphical text editor you prefer. For instance, to edit it withnano
, use:nano ~/.bashrc
-
At the end of the file, add the
ping
function:
function ping() {
local new_args=()
local url_found=0
for arg in "$@"; do
if [[ "$arg" =~ ^http ]] && [ $url_found -eq 0 ]; then
# Process the URL
local url="$arg"
url=${url#*://} # Remove protocol
url=${url%%/*} # Remove path
url=${url%%:*} # Remove port
url=${url%%@*} # Remove user info
new_args+=("$url")
url_found=1
else
# Add the argument as is
new_args+=("$arg")
fi
done
# Call the original ping command with the new argument list
command ping "${new_args[@]}"
}
-
Save the file and exit the editor. In
nano
, you do this by pressingCTRL + X
, thenY
to confirm, andEnter
to save. -
To make the changes take effect in your current terminal session, source the
.bashrc
file:source ~/.bashrc
-
Open your
.zshrc
file in a text editor, like so:nano ~/.zshrc
-
At the end of the file, add the same
ping
function:
function ping() {
local new_args=()
local url_found=0
for arg in "$@"; do
if [[ "$arg" =~ ^http ]] && [ $url_found -eq 0 ]; then
# Process the URL
local url="$arg"
url=${url#*://} # Remove protocol
url=${url%%/*} # Remove path
url=${url%%:*} # Remove port
url=${url%%@*} # Remove user info
new_args+=("$url")
url_found=1
else
# Add the argument as is
new_args+=("$arg")
fi
done
# Call the original ping command with the new argument list
command ping "${new_args[@]}"
}
-
Save and exit the editor.
-
To apply the changes, source the
.zshrc
file:source ~/.zshrc
Now, the modified ping
function will be available in any new terminal session. Note that this will override the default ping
command in your shell, so make sure this is the behavior you want. If you ever wish to revert to the original ping
command, simply remove these lines from your .bashrc
or .zshrc
and source the file again.