Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2003
    Posts
    6

    Unanswered: sed results as a variable

    I need to scan a file and pull the first line of data. I need to use this result later in the shell script.

    Right now I am using:

    sed -n 1,1p invoice.tst

    This works but I can't seem to redirect the output to a variable. I've tried....

    sed -n 1,1p invoice.tst $VAR
    and a few other formats....

    any thoughts out there?

    Thanks,
    Doug.

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    head -1 yourFile | read theVariable

    or


    theVariable=$(head -1 yourFile)

    or

    ...


    HTH

  3. #3
    Join Date
    Jul 2003
    Posts
    6

    Smile

    Works great thanks,

    Doug.

  4. #4
    Join Date
    Jul 2003
    Location
    Edinburgh
    Posts
    35
    Originally posted by Damian Ibbotson
    theVariable=$(head -1 yourFile)
    alternatively with backquotes

    theVariable=`head -1 yourFile`

    I've been away from shell scripting for a while and now I'm seeing more constructs like your reply than the backquotes style. Is there any reason to favour one over the other? Does the former not have to spawn a sub-shell or something?

    Thanks.

  5. #5
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by chillies
    alternatively with backquotes

    theVariable=`head -1 yourFile`

    I've been away from shell scripting for a while and now I'm seeing more constructs like your reply than the backquotes style. Is there any reason to favour one over the other? Does the former not have to spawn a sub-shell or something?

    Thanks.
    I guess it's mainly due to the rise and rise of Linux (and hence Bash). The clear advantage of $() over `` is that it can be nested (e.g. $( $() ) ), whereas backticks cannot.

Posting Permissions

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