0

I have a script like this:

#!/bin/bash                                                                                                                                                                                                                            
                                                                                                                                                                                                                                   
REGEX='(www\.)?\w*\.\w*'                                                                                                                                                                                                               
                                                                                                                                                                                                                                   
echo "$1" | grep -P -o $REGEX                                                                                                                                                                                                          
                                                                                                                                                                                                                                   
DOMAIN="$1" | grep -P -o $REGEX                                                                                                                                                                                                        

echo $DOMAIN    

What I want to achieve is to get the domain from a full URL.

If I give as parameter the: http://example.com/ then I need the example.com

The first echo works.

How could I pass that expression into a variable? I need to pass later the DOMAIN to a script.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • 1
    As the [tag:bash] tag you used instructs - "For shell scripts with syntax or other errors, please check them at https://shellcheck.net before posting them here." – Ed Morton Jun 25 '23 at 13:28
  • 1
    assuming the objective is to capture the results of the `echo | grep` in the variable `DOMAIN` ... `DOMAIN=$(echo "$1" | grep -P -o $REGEX)` – markp-fuso Jun 25 '23 at 13:51
  • @markp-fuso Thank you, it should be an answer.... – vaso123 Jun 25 '23 at 14:07
  • 1
    @vaso123 `var=$(command)` is a pretty basic concept in (unix/linux) shell scripting; not really worthy of an answer; I *would* recommend reviewing the linked 'duplicates' for other, more efficient, methods of parsing a string; good luck – markp-fuso Jun 25 '23 at 14:14

0 Answers0