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.

4
  • 1
    As the bash tag you used instructs - "For shell scripts with syntax or other errors, please check them at shellcheck.net before posting them here."
    – Ed Morton
    Commented Jun 25, 2023 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
    Commented Jun 25, 2023 at 13:51
  • @markp-fuso Thank you, it should be an answer....
    – vaso123
    Commented Jun 25, 2023 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
    Commented Jun 25, 2023 at 14:14

0

Browse other questions tagged or ask your own question.