Find the answer to your Linux question:
Results 1 to 3 of 3
I can print a specific line of a file with: $ sed -n '20p' myFile How can I store it in a variable (in a shell script)? (I wasn't successful ...
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    12

    how can I assign a variable to a linux command

    I can print a specific line of a file with:
    $ sed -n '20p' myFile

    How can I store it in a variable (in a shell script)?

    (I wasn't successful with "myVar=sed -n '20p' myFile" for example)

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Posts
    25
    Code:
    myVar=`sed -n '20p' myFile`
    Will set it the first time, but you need to do that on each iteration.

    That's old school. The newer way is

    Code:
    myVar=$(sed -n '20p' myFile)

  3. #3
    Just Joined! jr0sco's Avatar
    Join Date
    Aug 2010
    Location
    Australia
    Posts
    41
    Backward quote (`): Commands within backward quotes are executed and their output substituted into that location.

    Code:
    foo=`echo bar`
    echo $foo
    Output:
    bar

Posting Permissions

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