Bash: change git remote from SSH to HTTPS within a given dir
Extracting remote urls

Parsing the config file is error-prone. A much better way to extract the information about remotes is using Git commands. This will simplify your script a great deal.

Limitations

Be aware that the script as it is will not be able to convert remotes that were created using URL shorthands. For example, I have this in my global .gitconfig:

[url "git@github.com:"]
    insteadOf = "gh:"


This lets me clone a GitHub repo with git clone gh:user/repo instead of git clone git@github.com:user/repo. The config file will be written using the shorthand instead of the real URL, so your script will not be able to pick up the pattern.

You can easily solve this limitation by using Git commands to extract Git remote URLs.

String transformations

Instead of echo ... | sed ..., it's better to use here-strings:

sed ... &lt;&lt;&lt; ...


But when the text transformations are simple enough, and Bash's parameter expansion can handle it, that's even better. For example, instead of this:


  user_name=$(echo "$repo_url" | sed -Ene's#git@github.com:([^/]*)/(.*).git#\1#p')



This would be better:

# chop off the beginning until the first :
user_name=${repo_url#*:}
# chop off everything after the first /
user_name=${user_name%%/*}


Exiting with error

There are multiple exit points in the validate_path function.
Without parameter, exit will use the exit code of the previous command, and if that was echo, it's likely to be success,
when in fact you probably want to exit with error instead.

Usability

Reading paths inside a script is not user-friendly,
because you cannot use tab-completion.
It would be better to let users pass paths as parameters to the script.