Results 1 to 5 of 5

Thread: [SOLVED] 'expr' command problems

  1. #1
    Join Date
    Jun 2008
    Location
    Durban
    Beans
    13
    Distro
    Ubuntu 10.10 Maverick Meerkat

    [SOLVED] 'expr' command problems

    For some reason I'm unable to get expr to work with any parameters on strings that contain spaces. For example,


    This works:
    Code:
    $ temp='Fantastic'
    $ expr substr $temp 1 3
    Fan

    Whereas this doesn't:
    Code:
    $ temp='Fantastic work'
    $ expr substr $temp 1 3
    expr: syntax error
    The only difference is the added space. This problem occurs for all parameters of expr, including the length parameter.
    Suggestions welcome.

  2. #2
    Join Date
    Nov 2007
    Location
    UK
    Beans
    772
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: 'expr' command problems

    temp = "white space in string"
    echo temp: $temp
    Disclaimer: Yes I usually talk crap

  3. #3
    Join Date
    Jun 2008
    Location
    Durban
    Beans
    13
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: 'expr' command problems

    Quote Originally Posted by pedro_orange View Post
    temp = "white space in string"
    echo temp: $temp
    In the way that I originally posted, I am able to echo the variable without problems. It's only once I pass it to expr that I have a problem.

  4. #4
    Join Date
    Nov 2007
    Location
    UK
    Beans
    772
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: 'expr' command problems

    Aye spaces are weird in bash script variables.

    Quotes seem to act differently in interactive shells compared to automated.

    There is some method to do it, however it's been a long time since I've written a script.
    I'm pretty sure having double quotes around it in a script makes a difference, but that might just be to do with $variables within the "s.
    Disclaimer: Yes I usually talk crap

  5. #5
    Join Date
    Jun 2008
    Location
    Durban
    Beans
    13
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Smile Re: 'expr' command problems

    Thanks! You put me onto something, and after a quick Google I came across a solution here. The following taken from the aforementioned site:

    Code:
    if [ -f $filename ] translates $filename - if it has spaces the shell "thinks" $filename
             is really separate values  with no spaces
    if [ -f "$filename" ] translates $filename - if it has spaces the shell "thinks" $filename
             is realy one single value, spaces included
    if [ -f '$filename' ] does not translate filename - the shell takes the $ literally, not
             as a shell token meaning return the value of a variable
    So basically, my problem is solved by adding double quotes around the variable name:

    Code:
    $ temp='Fantastic work'
    $ expr substr "$temp" 1 3
    Fan

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
  •