Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:10:31+00:00 2026-06-15T13:10:31+00:00

This script should accept a set of search terms and return a formatted URL

  • 0

This script should accept a set of search terms and return a formatted URL to search google.

$ ./google_search.sh albert einstein
https://www.google.com/search?q=albert+einstein

It does that just fine, so I decided to add an option to search a specific site, or ignore that site, using a -s or -S flag.

$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein+site%3Awikipedia.org

This works the first time you run the script, but fails on every following attempt.

$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein
$ ./google_search.sh -s wikipedia.org albert einstein
https://www.google.com/search?q=albert+einstein

Opening a new Terminal window or restarting Terminal both clear this problem and allow one more attempt before failing.

The script:

#!/bin/bash

# original source of concatenate_args function by Tyilo:
# http://stackoverflow.com/questions/9354847/concatenate-inputs-in-bash-script
function concatenate_args
{
    string=""
    ignorenext=0
    for a in "$@" # Loop over arguments
    do
        if [[ "${a:0:1}" != "-" && $ignorenext = 0 ]] # Ignore flags (first character is -)
        then
            if [[ "$string" != "" ]]
            then
                string+="+" # Delimeter
            fi
            string+="$a"
        elif [[ $ignorenext = 1 ]]
        then
            ignorenext=0
        else
            ignorenext=1
        fi
    done
    echo "$string"
}

qry="$(concatenate_args "$@")"
glink="https://www.google.com/search?q="

site=""
while getopts :s:S: opt; do
    case $opt in
        s) site="+site%3A$OPTARG" ;; 
        S) site="+-site%3A$OPTARG" ;; 
    esac
done

url=$glink$qry$site

echo $url
# open -a Firefox $url

What needs to change to make this script more reliable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:10:32+00:00Added an answer on June 15, 2026 at 1:10 pm

    This is behaving like your are sourcing the script instead of executing it. If you use a dot and a space before the script, it causes the script to be executed line by line inside your current shell instead of creating a new shell. This allows environment variables that are changed inside the script to leak into your current shell’s environment, which can make one run of the script not behave the same as the next.

    In this case, it appears to be the use of getopts. getopts updates an environment variable each time it is called so it can keep track of which argument is being examined. The second time you source the script, it thinks that all the arguments have already been examined, so your argument ends up being ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This simple script should theoretically check the form for errors and then print any
I have one shell script opening a perl script. This perl script should be
This would be an example of database table: PHP script should limit query result
Should this work? <script type=text/javascript> function load_i () { img = new Image(); img.onload
EDIT: My main code no longer works, should this function work? <script type=text/javascript src=jquery-1.7.2.js></script>
I'm currently testing a script on my host. This script is to accept PayPal
I use the following PHP script as index for my website. This script should
I've a basic php script set up on a web server to accept xml
When building code like this: <script type=text/javascript src=<%=ResolveUrl(~/js/js.js)%>></script> or <input type=image src=<%=ResolveUrl(~/img/submit.png)%> /> Should
This script saves the files as /home/name/main/all my files and stuff . I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.