Search code examples
bashshellcut

How to manipulate the & special character in shell


I want to manipulate URLs in a shell script. I need to cut the URL with the & delimiter, and get the corresponding string.

I tried example="$(cut -d'&' -f2- <<< $1)" but when I'm executing this code and trying to echo $example, it wants to execute the $example content.

Can someone help me ?


Solution

  • No it does not.

    When you do:

    example="$(cut -d'&' -f2- <<< $1)"
    

    it tries to execute the cut. As a test:

    ljm@verlaine[~]$ a='1&ls&3&4'
    ljm@verlaine[~]$ example="$(cut -d'&' -f2- <<< $a)"
    ljm@verlaine[~]$ echo $example
    ls&3&4
    ljm@verlaine[~]$ 
    

    And, although quoting like iBug suggested is a good idea and best practice, it is not absolutely required here.