Results 1 to 10 of 10

Thread: Slash in variable

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Beans
    323

    Slash in variable

    Hi,
    I am writing following script:
    Code:
    huangyingw@laptop:~/bashrc$ cat fr.sh
    #! /bin/sh
    file=/home/huangyingw/myproject/git/test_fr/makefile
    sed -i "s/"$1"/"$2"/gi" $file
    huangyingw@laptop:~/bashrc$
    And passing following parameter to it, then I got the error.
    Code:
    huangyingw@laptop:~/myproject/git/test_fr$ fr ~/myproject/git/makefile/GNU_makefile_template ~/myproject/git/GNU_makefile_template
    sed: -e expression #1, char 9: unknown option to `s'
    huangyingw@laptop:~/myproject/git/test_fr$
    I think it is caused by the special character "Slash" in the parameter.
    How could I work around this?

  2. #2
    Join Date
    Sep 2006
    Beans
    8,623
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Slash in variable

    Quotes don't nest and it looks like you had them nested. Try different quotes instead.

    Code:
    sed -i 's/"$1"/"$2"/gi' $file
    Last edited by Lars Noodén; September 29th, 2012 at 01:27 PM.

  3. #3
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,264
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Slash in variable

    I'm not sure what those inner quotes are meant to be doing. Are they intentionally part of the pattern? If so, for this particular case, this should get around your slash problem.
    Code:
    sed -i "s#\"$1\"#\"$2\"#gi" $file
    However, if $1 or $2 contains a # then you will get the same problem.

    You would have to update your variables to escape whatever character you use as the separator.

    @ Lars Noodén If a single quote is used for the outer quote, then $1 and $2 are not subsituted by the shell.

  4. #4
    Join Date
    Jan 2009
    Beans
    323

    Re: Slash in variable

    Quote Originally Posted by spjackson View Post
    I'm not sure what those inner quotes are meant to be doing. Are they intentionally part of the pattern? If so, for this particular case, this should get around your slash problem.
    Code:
    sed -i "s#\"$1\"#\"$2\"#gi" $file
    However, if $1 or $2 contains a # then you will get the same problem.

    You would have to update your variables to escape whatever character you use as the separator.

    @ Lars Noodén If a single quote is used for the outer quote, then $1 and $2 are not subsituted by the shell.
    I have tried both of above solution, neither of them works
    Code:
    huangyingw@laptop:~/bashrc$ cat fr.sh
    #! /bin/sh
    file=/home/huangyingw/myproject/git/test_fr/makefile
    #sed -i "s/"$1"/"$2"/gi" $file
    #sed -i 's/"$1"/"$2"/gi' $file
    sed -i "s#\"$1\"#\"$2\"#gi" $file
    ....

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Slash in variable

    spjackson asked if the quotes are a part of the string (s/"something"/"something else"/). that would require escaping literal quotes and he he showed how to obtain it (\")

    in case there are no quotes to worry about, the whole thing is straightforward

    Code:
    sed -i "s#$1#$2#gi" "$file"
    # will solve slash issue, but you can use any character as a delimiter. It's best to use one that can't possibly exist in patterns and replacements.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  6. #6
    Join Date
    Jan 2009
    Beans
    323

    Re: Slash in variable

    Quote Originally Posted by Vaphell View Post
    spjackson asked if the quotes are a part of the string (s/"something"/"something else"/). that would require escaping literal quotes and he he showed how to obtain it (\")

    in case there are no quotes to worry about, the whole thing is straightforward

    Code:
    sed -i "s#$1#$2#gi" "$file"
    # will solve slash issue, but you can use any character as a delimiter. It's best to use one that can't possibly exist in patterns and replacements.
    I have tried this:
    Code:
    huangyingw@laptop:~/bashrc$ cat fr.sh
    #! /bin/sh
    file=/home/huangyingw/myproject/git/test_fr/makefile
    #sed -i "s/"$1"/"$2"/gi" $file
    #sed -i 's/"$1"/"$2"/gi' $file
    #sed -i "s#\"$1\"#\"$2\"#gi" $file
    sed -i "s#$1#$2#gi" "$file"
    But nothing change after I use in this way:
    Code:
    huangyingw@laptop:~/myproject/git/test_fr$ fr ~/myproject/git/makefile/GNU_makefile_template temp
    huangyingw@laptop:~/myproject/git/test_fr$ gs
    # On branch master
    nothing to commit (working directory clean)
    the git status command tell me that, nothing changed. And I have actually open the target file to check.

Tags for this Thread

Bookmarks

Posting Permissions

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