Shell script to open a url through terminal
If you are a developer then fixing bugs takes time because you have to shift from terminal to your fav browser & then type your query. (and then in between you open reddit/twitter π for some reason, which wanders your attention).
We can lighten this process a little bit by writing a shell script which will open a URL directly in the browser.
#!/usr/bin/env bash
# Simple script to invoke browser using terminal & search
#
# Browsers :
# chromium-browser <url>
# firefox --new-tab <url>
#
# Search Engines :
# Google: https://www.google.com/search?q=<string to search>
# DuckDuckGo: https://duckduckgo.com/?q=<search_term>
# DuckDuckGo Lite: https://lite.duckduckgo.com/lite/?q=<search_term>
# GitHub: https://github.com/search?q=<string to search>&ref=opensearch
urlencode() {
local string="${1}"
local encoded=""
local pos c o
for (( pos=0 ; pos<${#string} ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] )
# these characters are url safe (permitted)
o="${c}" ;;
* )
# Encode special characters
# Assign output to o, instead of printing to console
# %02x converts the character into hexadecimal notation.
printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
# return "encoded"
echo "${encoded}"
}
read -p "Search Query : " -r search_term
read -p "[F]irefox or [C]hromium ? : " -n 1 -r BROWSER_CHOICE
BROWSER_CHOICE=${BROWSER_CHOICE:-c}
case $BROWSER_CHOICE in
[f/F]* ) firefox --new-tab "duckduckgo.com/?q=$(urlencode "${search_term}")"
exit;;
[c/C]* ) chromium-browser "duckduckgo.com/?q=$(urlencode "${search_term}")"
exit ;;
* ) printf "\n%s\n" "[β]Invalid Input π, Try Again";;
esac
You can also view other options to the firefox
command by invoking help.
firefox --help
And if you have βchromiumβ installed
chromium-browser --help
Note : If you are on Debian, the command
gnome-www-browser
will point to the default browser which comes in with your OS, i.e firefox. So both the commands can be easily swapped.
Save this somewhere in your scripts folder and add an alias for it.
alias ddg="/path/to/script.sh"
alias helpme="/path/to/script.sh"
alias search="/path/to/script.sh"
Hope you learned something new today. Take Care π€