Miscellaneous
  Home arrow Miscellaneous arrow Page 4 - Data Streams and the UNIX Shell
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Data Streams and the UNIX Shell
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-05-13

    Table of Contents:
  • Data Streams and the UNIX Shell
  • Pipes
  • Redirection and the Here Document
  • Saving Streams into Variables

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Data Streams and the UNIX Shell - Saving Streams into Variables


    (Page 4 of 4 )

    Now you can play around with the input and the output as long as they are files. The question is, how can we can save the data stream of a command inside a variable? A variable is a data stream store that lives as long as your program/script. You can save the output of a command to a variable in two ways.

    There is an older, more traditional syntax, and a new one which is more modern. You can use either of them as you wish. The traditional syntax is as follows:

    variable=`command line`

    Here we use the back tick to surround our command line. The modern syntax is just a little different:

    variable=$(command line)

    The result is the same whatever you use. However, the second one is easier to read, so I recommend that you that. For example, we can save the first two lines of a file with the following command:

    #traditional

    firstLines='head -2 alfa.txt'

     

    #modern

    firstLines=$(head -2 alfa.txt)

    The shell will rewrite every new line character to a space in order to fit everything inside the variable and into a single line. The shell will also rewrite the command line before it executes it, if there are any meta-characters. Besides the one presented in my previous article, there are also the following related to the directories:

    *

    It will replace it with the names of the directories inside the working directory

    ~

    The path to the users home directory ( for example /home/gaborj)

    If we want to use them in their literal meaning, you will need to surround it with the forward tick or add a backslash before them. This is all you need to learn for now about streams inside the shell. Rate my articles if you consider that they were worth your attention. Do not hesitate to ask your questions in the blog following the article.

    In the future, we will write more and more scripts inside the shell. Therefore, I need to ask you to make yourself familiar with one of the text editors inside the terminal. If you want something easy to use and learn, I recommend Joe. Nevertheless, if you want to learn to use something more advanced, a tool from which you can benefit in the future, VI Improved is the way to go. You can read a nice article about it here. We will continue next week with some control mechanisms of the shell. Until that arrives, remember to Live With Passion!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek